Turn org-mode into Org or Org mode
[org-mode.git] / lisp / ox-texinfo.el
blobaec9b0beb21fd6b57feda8a0c952f3f3660e5fec
1 ;;; ox-texinfo.el --- Texinfo Back-End for Org Export Engine -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2012-2016 Free Software Foundation, Inc.
4 ;; Author: Jonathan Leech-Pepin <jonathan.leechpepin at gmail dot com>
5 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs 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 ;; GNU Emacs 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;; See Org manual for details.
26 ;;; Code:
28 (require 'cl-lib)
29 (require 'ox)
31 (defvar orgtbl-exp-regexp)
35 ;;; Define Back-End
37 (org-export-define-backend 'texinfo
38 '((bold . org-texinfo-bold)
39 (center-block . org-texinfo-center-block)
40 (clock . org-texinfo-clock)
41 (code . org-texinfo-code)
42 (drawer . org-texinfo-drawer)
43 (dynamic-block . org-texinfo-dynamic-block)
44 (entity . org-texinfo-entity)
45 (example-block . org-texinfo-example-block)
46 (export-block . org-texinfo-export-block)
47 (export-snippet . org-texinfo-export-snippet)
48 (fixed-width . org-texinfo-fixed-width)
49 (footnote-definition . org-texinfo-footnote-definition)
50 (footnote-reference . org-texinfo-footnote-reference)
51 (headline . org-texinfo-headline)
52 (inline-src-block . org-texinfo-inline-src-block)
53 (inlinetask . org-texinfo-inlinetask)
54 (italic . org-texinfo-italic)
55 (item . org-texinfo-item)
56 (keyword . org-texinfo-keyword)
57 (line-break . org-texinfo-line-break)
58 (link . org-texinfo-link)
59 (node-property . org-texinfo-node-property)
60 (paragraph . org-texinfo-paragraph)
61 (plain-list . org-texinfo-plain-list)
62 (plain-text . org-texinfo-plain-text)
63 (planning . org-texinfo-planning)
64 (property-drawer . org-texinfo-property-drawer)
65 (quote-block . org-texinfo-quote-block)
66 (radio-target . org-texinfo-radio-target)
67 (section . org-texinfo-section)
68 (special-block . org-texinfo-special-block)
69 (src-block . org-texinfo-src-block)
70 (statistics-cookie . org-texinfo-statistics-cookie)
71 (strike-through . org-texinfo-strike-through)
72 (subscript . org-texinfo-subscript)
73 (superscript . org-texinfo-superscript)
74 (table . org-texinfo-table)
75 (table-cell . org-texinfo-table-cell)
76 (table-row . org-texinfo-table-row)
77 (target . org-texinfo-target)
78 (template . org-texinfo-template)
79 (timestamp . org-texinfo-timestamp)
80 (underline . org-texinfo-underline)
81 (verbatim . org-texinfo-verbatim)
82 (verse-block . org-texinfo-verse-block))
83 :filters-alist
84 '((:filter-headline . org-texinfo--filter-section-blank-lines)
85 (:filter-parse-tree . org-texinfo--normalize-headlines)
86 (:filter-section . org-texinfo--filter-section-blank-lines))
87 :menu-entry
88 '(?i "Export to Texinfo"
89 ((?t "As TEXI file" org-texinfo-export-to-texinfo)
90 (?i "As INFO file" org-texinfo-export-to-info)
91 (?o "As INFO file and open"
92 (lambda (a s v b)
93 (if a (org-texinfo-export-to-info t s v b)
94 (org-open-file (org-texinfo-export-to-info nil s v b)))))))
95 :options-alist
96 '((:texinfo-filename "TEXINFO_FILENAME" nil nil t)
97 (:texinfo-class "TEXINFO_CLASS" nil org-texinfo-default-class t)
98 (:texinfo-header "TEXINFO_HEADER" nil nil newline)
99 (:texinfo-post-header "TEXINFO_POST_HEADER" nil nil newline)
100 (:subtitle "SUBTITLE" nil nil parse)
101 (:subauthor "SUBAUTHOR" nil nil newline)
102 (:texinfo-dircat "TEXINFO_DIR_CATEGORY" nil nil t)
103 (:texinfo-dirtitle "TEXINFO_DIR_TITLE" nil nil t)
104 (:texinfo-dirdesc "TEXINFO_DIR_DESC" nil nil t)
105 (:texinfo-printed-title "TEXINFO_PRINTED_TITLE" nil nil t)
106 ;; Other variables.
107 (:texinfo-classes nil nil org-texinfo-classes)
108 (:texinfo-format-headline-function nil nil org-texinfo-format-headline-function)
109 (:texinfo-node-description-column nil nil org-texinfo-node-description-column)
110 (:texinfo-active-timestamp-format nil nil org-texinfo-active-timestamp-format)
111 (:texinfo-inactive-timestamp-format nil nil org-texinfo-inactive-timestamp-format)
112 (:texinfo-diary-timestamp-format nil nil org-texinfo-diary-timestamp-format)
113 (:texinfo-link-with-unknown-path-format nil nil org-texinfo-link-with-unknown-path-format)
114 (:texinfo-tables-verbatim nil nil org-texinfo-tables-verbatim)
115 (:texinfo-table-scientific-notation nil nil org-texinfo-table-scientific-notation)
116 (:texinfo-def-table-markup nil nil org-texinfo-def-table-markup)
117 (:texinfo-text-markup-alist nil nil org-texinfo-text-markup-alist)
118 (:texinfo-format-drawer-function nil nil org-texinfo-format-drawer-function)
119 (:texinfo-format-inlinetask-function nil nil org-texinfo-format-inlinetask-function)))
123 ;;; User Configurable Variables
125 (defgroup org-export-texinfo nil
126 "Options for exporting Org mode files to Texinfo."
127 :tag "Org Export Texinfo"
128 :version "24.4"
129 :package-version '(Org . "8.0")
130 :group 'org-export)
132 ;;;; Preamble
134 (defcustom org-texinfo-coding-system nil
135 "Default document encoding for Texinfo output.
137 If nil it will default to `buffer-file-coding-system'."
138 :group 'org-export-texinfo
139 :type 'coding-system)
141 (defcustom org-texinfo-default-class "info"
142 "The default Texinfo class."
143 :group 'org-export-texinfo
144 :type '(string :tag "Texinfo class"))
146 (defcustom org-texinfo-classes
147 '(("info"
148 "@documentencoding AUTO\n@documentlanguage AUTO"
149 ("@chapter %s" . "@unnumbered %s")
150 ("@section %s" . "@unnumberedsec %s")
151 ("@subsection %s" . "@unnumberedsubsec %s")
152 ("@subsubsection %s" . "@unnumberedsubsubsec %s")))
153 "Alist of Texinfo classes and associated header and structure.
154 If #+TEXINFO_CLASS is set in the buffer, use its value and the
155 associated information. Here is the structure of each cell:
157 (class-name
158 header-string
159 (numbered-section . unnumbered-section)
160 ...)
163 The header string
164 -----------------
166 The header string is inserted in the header of the generated
167 document, right after \"@setfilename\" and \"@settitle\"
168 commands.
170 If it contains the special string
172 \"@documentencoding AUTO\"
174 \"AUTO\" will be replaced with an appropriate coding system. See
175 `org-texinfo-coding-system' for more information. Likewise, if
176 the string contains the special string
178 \"@documentlanguage AUTO\"
180 \"AUTO\" will be replaced with the language defined in the
181 buffer, through #+LANGUAGE keyword, or globally, with
182 `org-export-default-language', which see.
185 The sectioning structure
186 ------------------------
188 The sectioning structure of the class is given by the elements
189 following the header string. For each sectioning level, a number
190 of strings is specified. A %s formatter is mandatory in each
191 section string and will be replaced by the title of the section.
193 Instead of a list of sectioning commands, you can also specify
194 a function name. That function will be called with two
195 parameters, the reduced) level of the headline, and a predicate
196 non-nil when the headline should be numbered. It must return
197 a format string in which the section title will be added."
198 :group 'org-export-texinfo
199 :version "24.4"
200 :package-version '(Org . "8.2")
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-texinfo-format-headline-function
214 'org-texinfo-format-headline-default-function
215 "Function to format headline text.
217 This function will be called with 5 arguments:
218 TODO the todo keyword (string or nil).
219 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
220 PRIORITY the priority of the headline (integer or nil)
221 TEXT the main headline text (string).
222 TAGS the tags as a list of strings (list of strings or nil).
224 The function result will be used in the section format string."
225 :group 'org-export-texinfo
226 :type 'function
227 :version "25.1"
228 :package-version '(Org . "8.3"))
230 ;;;; Node listing (menu)
232 (defcustom org-texinfo-node-description-column 32
233 "Column at which to start the description in the node listings.
234 If a node title is greater than this length, the description will
235 be placed after the end of the title."
236 :group 'org-export-texinfo
237 :type 'integer)
239 ;;;; Timestamps
241 (defcustom org-texinfo-active-timestamp-format "@emph{%s}"
242 "A printf format string to be applied to active timestamps."
243 :group 'org-export-texinfo
244 :type 'string)
246 (defcustom org-texinfo-inactive-timestamp-format "@emph{%s}"
247 "A printf format string to be applied to inactive timestamps."
248 :group 'org-export-texinfo
249 :type 'string)
251 (defcustom org-texinfo-diary-timestamp-format "@emph{%s}"
252 "A printf format string to be applied to diary timestamps."
253 :group 'org-export-texinfo
254 :type 'string)
256 ;;;; Links
258 (defcustom org-texinfo-link-with-unknown-path-format "@indicateurl{%s}"
259 "Format string for links with unknown path type."
260 :group 'org-export-texinfo
261 :type 'string)
263 ;;;; Tables
265 (defcustom org-texinfo-tables-verbatim nil
266 "When non-nil, tables are exported verbatim."
267 :group 'org-export-texinfo
268 :type 'boolean)
270 (defcustom org-texinfo-table-scientific-notation "%s\\,(%s)"
271 "Format string to display numbers in scientific notation.
272 The format should have \"%s\" twice, for mantissa and exponent
273 \(i.e. \"%s\\\\times10^{%s}\").
275 When nil, no transformation is made."
276 :group 'org-export-texinfo
277 :type '(choice
278 (string :tag "Format string")
279 (const :tag "No formatting" nil)))
281 (defcustom org-texinfo-def-table-markup "@samp"
282 "Default setting for @table environments."
283 :group 'org-export-texinfo
284 :type 'string)
286 ;;;; Text markup
288 (defcustom org-texinfo-text-markup-alist '((bold . "@strong{%s}")
289 (code . code)
290 (italic . "@emph{%s}")
291 (verbatim . verb))
292 "Alist of Texinfo expressions to convert text markup.
294 The key must be a symbol among `bold', `code', `italic',
295 `strike-through', `underscore' and `verbatim'. The value is
296 a formatting string to wrap fontified text with.
298 Value can also be set to the following symbols: `verb' and
299 `code'. For the former, Org will use \"@verb\" to create
300 a format string and select a delimiter character that isn't in
301 the string. For the latter, Org will use \"@code\" to typeset
302 and try to protect special characters.
304 If no association can be found for a given markup, text will be
305 returned as-is."
306 :group 'org-export-texinfo
307 :type 'alist
308 :options '(bold code italic strike-through underscore verbatim))
310 ;;;; Drawers
312 (defcustom org-texinfo-format-drawer-function (lambda (_name contents) contents)
313 "Function called to format a drawer in Texinfo code.
315 The function must accept two parameters:
316 NAME the drawer name, like \"LOGBOOK\"
317 CONTENTS the contents of the drawer.
319 The function should return the string to be exported.
321 The default function simply returns the value of CONTENTS."
322 :group 'org-export-texinfo
323 :version "24.4"
324 :package-version '(Org . "8.2")
325 :type 'function)
327 ;;;; Inlinetasks
329 (defcustom org-texinfo-format-inlinetask-function
330 'org-texinfo-format-inlinetask-default-function
331 "Function called to format an inlinetask in Texinfo code.
333 The function must accept six parameters:
334 TODO the todo keyword, as a string
335 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
336 PRIORITY the inlinetask priority, as a string
337 NAME the inlinetask name, as a string.
338 TAGS the inlinetask tags, as a list of strings.
339 CONTENTS the contents of the inlinetask, as a string.
341 The function should return the string to be exported."
342 :group 'org-export-texinfo
343 :type 'function)
345 ;;;; Compilation
347 (defcustom org-texinfo-info-process '("makeinfo %f")
348 "Commands to process a Texinfo file to an INFO file.
349 This is list of strings, each of them will be given to the shell
350 as a command. %f in the command will be replaced by the full
351 file name, %b by the file base name (i.e without extension) and
352 %o by the base directory of the file."
353 :group 'org-export-texinfo
354 :type '(repeat :tag "Shell command sequence"
355 (string :tag "Shell command")))
357 (defcustom org-texinfo-logfiles-extensions
358 '("aux" "toc" "cp" "fn" "ky" "pg" "tp" "vr")
359 "The list of file extensions to consider as Texinfo logfiles.
360 The logfiles will be remove if `org-texinfo-remove-logfiles' is
361 non-nil."
362 :group 'org-export-texinfo
363 :type '(repeat (string :tag "Extension")))
365 (defcustom org-texinfo-remove-logfiles t
366 "Non-nil means remove the logfiles produced by compiling a Texinfo file.
367 By default, logfiles are files with these extensions: .aux, .toc,
368 .cp, .fn, .ky, .pg and .tp. To define the set of logfiles to remove,
369 set `org-texinfo-logfiles-extensions'."
370 :group 'org-export-latex
371 :type 'boolean)
373 ;;; Constants
375 (defconst org-texinfo-max-toc-depth 4
376 "Maximum depth for creation of detailed menu listings.
377 Beyond this depth, Texinfo will not recognize the nodes and will
378 cause errors. Left as a constant in case this value ever
379 changes.")
381 (defconst org-texinfo-supported-coding-systems
382 '("US-ASCII" "UTF-8" "ISO-8859-15" "ISO-8859-1" "ISO-8859-2" "koi8-r" "koi8-u")
383 "List of coding systems supported by Texinfo, as strings.
384 Specified coding system will be matched against these strings.
385 If two strings share the same prefix (e.g. \"ISO-8859-1\" and
386 \"ISO-8859-15\"), the most specific one has to be listed first.")
388 (defconst org-texinfo-inline-image-rules
389 (list (cons "file"
390 (regexp-opt '("eps" "pdf" "png" "jpg" "jpeg" "gif" "svg"))))
391 "Rules characterizing image files that can be inlined.")
394 ;;; Internal Functions
396 (defun org-texinfo--filter-section-blank-lines (headline _backend _info)
397 "Filter controlling number of blank lines after a section."
398 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" "\n\n" headline))
400 (defun org-texinfo--normalize-headlines (tree _backend info)
401 "Normalize headlines in TREE.
403 BACK-END is the symbol specifying back-end used for export. INFO
404 is a plist used as a communication channel.
406 Make sure every headline in TREE contains a section, since those
407 are required to install a menu. Also put exactly one blank line
408 at the end of each section.
410 Return new tree."
411 (org-element-map tree 'headline
412 (lambda (hl)
413 (org-element-put-property hl :post-blank 1)
414 (let ((contents (org-element-contents hl)))
415 (when contents
416 (let ((first (org-element-map contents '(headline section)
417 #'identity info t)))
418 (unless (eq (org-element-type first) 'section)
419 (apply #'org-element-set-contents
421 (cons `(section (:parent ,hl)) contents)))))))
422 info)
423 tree)
425 (defun org-texinfo--find-verb-separator (s)
426 "Return a character not used in string S.
427 This is used to choose a separator for constructs like \\verb."
428 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
429 (cl-loop for c across ll
430 when (not (string-match (regexp-quote (char-to-string c)) s))
431 return (char-to-string c))))
433 (defun org-texinfo--text-markup (text markup _info)
434 "Format TEXT depending on MARKUP text markup.
435 INFO is a plist used as a communication channel. See
436 `org-texinfo-text-markup-alist' for details."
437 (pcase (cdr (assq markup org-texinfo-text-markup-alist))
438 ;; No format string: Return raw text.
439 (`nil text)
440 (`verb
441 (let ((separator (org-texinfo--find-verb-separator text)))
442 (concat "@verb{" separator text separator "}")))
443 (`code
444 (format "@code{%s}" (replace-regexp-in-string "[@{}]" "@\\&" text)))
445 ;; Else use format string.
446 (fmt (format fmt text))))
448 (defun org-texinfo--get-node (blob info)
449 "Return node or anchor associated to BLOB.
450 BLOB is an element or object. INFO is a plist used as
451 a communication channel. The function guarantees the node or
452 anchor name is unique."
453 (let ((cache (plist-get info :texinfo-node-cache)))
454 (or (cdr (assq blob cache))
455 (let ((name
456 (org-texinfo--sanitize-node
457 (if (eq (org-element-type blob) 'headline)
458 (org-export-data (org-export-get-alt-title blob info) info)
459 (org-export-get-reference blob info)))))
460 ;; Ensure NAME is unique.
461 (while (rassoc name cache) (setq name (concat name "x")))
462 (plist-put info :texinfo-node-cache (cons (cons blob name) cache))
463 name))))
465 (defun org-texinfo--sanitize-node (title)
466 "Bend string TITLE to node line requirements.
467 Trim string and collapse multiple whitespace characters as they
468 are not significant. Also remove the following characters: @
469 { } ( ) : . ,"
470 (replace-regexp-in-string
471 "[:,.]" ""
472 (replace-regexp-in-string
473 "\\`(\\(.*)\\)" "[\\1"
474 (org-trim (replace-regexp-in-string "[ \t]\\{2,\\}" " " title)))))
476 (defun org-texinfo--sanitize-content (text)
477 "Escape special characters in string TEXT.
478 Special characters are: @ { }"
479 (replace-regexp-in-string "[@{}]" "@\\&" text))
481 (defun org-texinfo--wrap-float (value info &optional type label caption short)
482 "Wrap string VALUE within a @float command.
483 INFO is the current export state, as a plist. TYPE is float
484 type, as a string. LABEL is the cross reference label for the
485 float, as a string. CAPTION and SHORT are, respectively, the
486 caption and shortcaption used for the float, as secondary
487 strings (e.g., returned by `org-export-get-caption')."
488 (let* ((backend
489 (org-export-create-backend
490 :parent 'texinfo
491 :transcoders '((link . (lambda (object c i) c))
492 (radio-target . (lambda (object c i) c))
493 (target . ignore))))
494 (short-backend
495 (org-export-create-backend
496 :parent 'texinfo
497 :transcoders '((footnote-reference . ignore)
498 (inline-src-block . ignore)
499 (link . (lambda (object c i) c))
500 (radio-target . (lambda (object c i) c))
501 (target . ignore)
502 (verbatim . ignore))))
503 (short-str
504 (if (and short caption)
505 (format "@shortcaption{%s}\n"
506 (org-export-data-with-backend short short-backend info))
507 ""))
508 (caption-str
509 (if (or short caption)
510 (format "@caption{%s}\n"
511 (org-export-data-with-backend
512 (or caption short)
513 (if (equal short-str "") short-backend backend)
514 info))
515 "")))
516 (format "@float %s%s\n%s\n%s%s@end float"
517 type (if label (concat "," label) "") value caption-str short-str)))
519 ;;; Template
521 (defun org-texinfo-template (contents info)
522 "Return complete document string after Texinfo conversion.
523 CONTENTS is the transcoded contents string. INFO is a plist
524 holding export options."
525 (let ((title (org-export-data (plist-get info :title) info))
526 ;; Copying data is the contents of the first headline in
527 ;; parse tree with a non-nil copying property.
528 (copying (org-element-map (plist-get info :parse-tree) 'headline
529 (lambda (hl)
530 (and (org-not-nil (org-element-property :COPYING hl))
531 (org-element-contents hl)))
532 info t)))
533 (concat
534 "\\input texinfo @c -*- texinfo -*-\n"
535 "@c %**start of header\n"
536 (let ((file (or (plist-get info :texinfo-filename)
537 (let ((f (plist-get info :output-file)))
538 (and f (concat (file-name-sans-extension f) ".info"))))))
539 (and file (format "@setfilename %s\n" file)))
540 (format "@settitle %s\n" title)
541 ;; Insert class-defined header.
542 (org-element-normalize-string
543 (let ((header (nth 1 (assoc (plist-get info :texinfo-class)
544 org-texinfo-classes)))
545 (coding
546 (catch 'coding-system
547 (let ((case-fold-search t)
548 (name (symbol-name (or org-texinfo-coding-system
549 buffer-file-coding-system))))
550 (dolist (system org-texinfo-supported-coding-systems "UTF-8")
551 (when (string-match-p (regexp-quote system) name)
552 (throw 'coding-system system))))))
553 (language (plist-get info :language))
554 (case-fold-search nil))
555 ;; Auto coding system.
556 (replace-regexp-in-string
557 "^@documentencoding \\(AUTO\\)$"
558 coding
559 (replace-regexp-in-string
560 "^@documentlanguage \\(AUTO\\)$" language header t nil 1) t nil 1)))
561 ;; Additional header options set by #+TEXINFO_HEADER.
562 (let ((texinfo-header (plist-get info :texinfo-header)))
563 (and texinfo-header (org-element-normalize-string texinfo-header)))
564 "@c %**end of header\n\n"
565 ;; Additional options set by #+TEXINFO_POST_HEADER.
566 (let ((texinfo-post-header (plist-get info :texinfo-post-header)))
567 (and texinfo-post-header
568 (org-element-normalize-string texinfo-post-header)))
569 ;; Copying.
570 (and copying
571 (format "@copying\n%s@end copying\n\n"
572 (org-element-normalize-string
573 (org-export-data copying info))))
574 ;; Info directory information. Only supply if both title and
575 ;; category are provided.
576 (let ((dircat (plist-get info :texinfo-dircat))
577 (dirtitle
578 (let ((title (plist-get info :texinfo-dirtitle)))
579 (and title
580 (string-match "^\\(?:\\* \\)?\\(.*?\\)\\(\\.\\)?$" title)
581 (format "* %s." (match-string 1 title))))))
582 (when (and dircat dirtitle)
583 (concat "@dircategory " dircat "\n"
584 "@direntry\n"
585 (let ((dirdesc
586 (let ((desc (plist-get info :texinfo-dirdesc)))
587 (cond ((not desc) nil)
588 ((string-match-p "\\.$" desc) desc)
589 (t (concat desc "."))))))
590 (if dirdesc (format "%-23s %s" dirtitle dirdesc) dirtitle))
591 "\n"
592 "@end direntry\n\n")))
593 ;; Title
594 "@finalout\n"
595 "@titlepage\n"
596 (when (plist-get info :with-title)
597 (concat
598 (format "@title %s\n" (or (plist-get info :texinfo-printed-title) title ""))
599 (let ((subtitle (plist-get info :subtitle)))
600 (when subtitle
601 (format "@subtitle %s\n"
602 (org-export-data subtitle info))))))
603 (when (plist-get info :with-author)
604 (concat
605 ;; Primary author.
606 (let ((author (org-string-nw-p
607 (org-export-data (plist-get info :author) info)))
608 (email (and (plist-get info :with-email)
609 (org-string-nw-p
610 (org-export-data (plist-get info :email) info)))))
611 (cond ((and author email)
612 (format "@author %s (@email{%s})\n" author email))
613 (author (format "@author %s\n" author))
614 (email (format "@author @email{%s}\n" email))))
615 ;; Other authors.
616 (let ((subauthor (plist-get info :subauthor)))
617 (and subauthor
618 (org-element-normalize-string
619 (replace-regexp-in-string "^" "@author " subauthor))))))
620 (and copying "@page\n@vskip 0pt plus 1filll\n@insertcopying\n")
621 "@end titlepage\n\n"
622 ;; Table of contents.
623 (and (plist-get info :with-toc) "@contents\n\n")
624 ;; Configure Top Node when not for Tex
625 "@ifnottex\n"
626 "@node Top\n"
627 (format "@top %s\n" title)
628 (and copying "@insertcopying\n")
629 "@end ifnottex\n\n"
630 ;; Menu.
631 (org-texinfo-make-menu (plist-get info :parse-tree) info 'master)
632 "\n"
633 ;; Document's body.
634 contents "\n"
635 ;; Creator.
636 (and (plist-get info :with-creator)
637 (concat (plist-get info :creator) "\n"))
638 ;; Document end.
639 "@bye")))
643 ;;; Transcode Functions
645 ;;;; Bold
647 (defun org-texinfo-bold (_bold contents info)
648 "Transcode BOLD from Org to Texinfo.
649 CONTENTS is the text with bold markup. INFO is a plist holding
650 contextual information."
651 (org-texinfo--text-markup contents 'bold info))
653 ;;;; Center Block
655 (defun org-texinfo-center-block (_center-block contents _info)
656 "Transcode a CENTER-BLOCK element from Org to Texinfo.
657 CONTENTS holds the contents of the block. INFO is a plist used
658 as a communication channel."
659 contents)
661 ;;;; Clock
663 (defun org-texinfo-clock (clock _contents info)
664 "Transcode a CLOCK element from Org to Texinfo.
665 CONTENTS is nil. INFO is a plist holding contextual
666 information."
667 (concat
668 "@noindent"
669 (format "@strong{%s} " org-clock-string)
670 (format (plist-get info :texinfo-inactive-timestamp-format)
671 (concat (org-timestamp-translate (org-element-property :value clock))
672 (let ((time (org-element-property :duration clock)))
673 (and time (format " (%s)" time)))))
674 "@*"))
676 ;;;; Code
678 (defun org-texinfo-code (code _contents info)
679 "Transcode a CODE object from Org to Texinfo.
680 CONTENTS is nil. INFO is a plist used as a communication
681 channel."
682 (org-texinfo--text-markup (org-element-property :value code) 'code info))
684 ;;;; Drawer
686 (defun org-texinfo-drawer (drawer contents info)
687 "Transcode a DRAWER element from Org to Texinfo.
688 CONTENTS holds the contents of the block. INFO is a plist
689 holding contextual information."
690 (let* ((name (org-element-property :drawer-name drawer))
691 (output (funcall (plist-get info :texinfo-format-drawer-function)
692 name contents)))
693 output))
695 ;;;; Dynamic Block
697 (defun org-texinfo-dynamic-block (_dynamic-block contents _info)
698 "Transcode a DYNAMIC-BLOCK element from Org to Texinfo.
699 CONTENTS holds the contents of the block. INFO is a plist
700 holding contextual information."
701 contents)
703 ;;;; Entity
705 (defun org-texinfo-entity (entity _contents _info)
706 "Transcode an ENTITY object from Org to Texinfo.
707 CONTENTS are the definition itself. INFO is a plist holding
708 contextual information."
709 (let ((ent (org-element-property :latex entity)))
710 (if (org-element-property :latex-math-p entity) (format "@math{%s}" ent) ent)))
712 ;;;; Example Block
714 (defun org-texinfo-example-block (example-block _contents info)
715 "Transcode an EXAMPLE-BLOCK element from Org to Texinfo.
716 CONTENTS is nil. INFO is a plist holding contextual
717 information."
718 (format "@verbatim\n%s@end verbatim"
719 (org-export-format-code-default example-block info)))
721 ;;; Export Block
723 (defun org-texinfo-export-block (export-block _contents _info)
724 "Transcode a EXPORT-BLOCK element from Org to Texinfo.
725 CONTENTS is nil. INFO is a plist holding contextual information."
726 (when (string= (org-element-property :type export-block) "TEXINFO")
727 (org-remove-indentation (org-element-property :value export-block))))
729 ;;; Export Snippet
731 (defun org-texinfo-export-snippet (export-snippet _contents _info)
732 "Transcode a EXPORT-SNIPPET object from Org to Texinfo.
733 CONTENTS is nil. INFO is a plist holding contextual information."
734 (when (eq (org-export-snippet-backend export-snippet) 'texinfo)
735 (org-element-property :value export-snippet)))
737 ;;;; Fixed Width
739 (defun org-texinfo-fixed-width (fixed-width _contents _info)
740 "Transcode a FIXED-WIDTH element from Org to Texinfo.
741 CONTENTS is nil. INFO is a plist holding contextual information."
742 (format "@example\n%s\n@end example"
743 (org-remove-indentation
744 (org-texinfo--sanitize-content
745 (org-element-property :value fixed-width)))))
747 ;;;; Footnote Reference
749 (defun org-texinfo-footnote-reference (footnote _contents info)
750 "Create a footnote reference for FOOTNOTE.
752 FOOTNOTE is the footnote to define. CONTENTS is nil. INFO is a
753 plist holding contextual information."
754 (let ((def (org-export-get-footnote-definition footnote info)))
755 (format "@footnote{%s}"
756 (org-trim (org-export-data def info)))))
758 ;;;; Headline
760 (defun org-texinfo-headline (headline contents info)
761 "Transcode a HEADLINE element from Org to Texinfo.
762 CONTENTS holds the contents of the headline. INFO is a plist
763 holding contextual information."
764 (let* ((class (plist-get info :texinfo-class))
765 (level (org-export-get-relative-level headline info))
766 (numberedp (org-export-numbered-headline-p headline info))
767 (class-sectioning (assoc class (plist-get info :texinfo-classes)))
768 ;; Find the index type, if any.
769 (index (org-element-property :INDEX headline))
770 ;; Create node info, to insert it before section formatting.
771 ;; Use custom menu title if present.
772 (node (format "@node %s\n" (org-texinfo--get-node headline info)))
773 ;; Section formatting will set two placeholders: one for the
774 ;; title and the other for the contents.
775 (section-fmt
776 (if (org-not-nil (org-element-property :APPENDIX headline))
777 "@appendix %s\n%s"
778 (let ((sec (if (and (symbolp (nth 2 class-sectioning))
779 (fboundp (nth 2 class-sectioning)))
780 (funcall (nth 2 class-sectioning) level numberedp)
781 (nth (1+ level) class-sectioning))))
782 (cond
783 ;; No section available for that LEVEL.
784 ((not sec) nil)
785 ;; Section format directly returned by a function.
786 ((stringp sec) sec)
787 ;; (numbered-section . unnumbered-section)
788 ((not (consp (cdr sec)))
789 (concat (if (or index (not numberedp)) (cdr sec) (car sec))
790 "\n%s"))))))
791 (todo
792 (and (plist-get info :with-todo-keywords)
793 (let ((todo (org-element-property :todo-keyword headline)))
794 (and todo (org-export-data todo info)))))
795 (todo-type (and todo (org-element-property :todo-type headline)))
796 (tags (and (plist-get info :with-tags)
797 (org-export-get-tags headline info)))
798 (priority (and (plist-get info :with-priority)
799 (org-element-property :priority headline)))
800 (text (org-export-data (org-element-property :title headline) info))
801 (full-text (funcall (plist-get info :texinfo-format-headline-function)
802 todo todo-type priority text tags))
803 (contents (if (org-string-nw-p contents) (concat "\n" contents) "")))
804 (cond
805 ;; Case 1: This is a footnote section: ignore it.
806 ((org-element-property :footnote-section-p headline) nil)
807 ;; Case 2: This is the `copying' section: ignore it
808 ;; This is used elsewhere.
809 ((org-not-nil (org-element-property :COPYING headline)) nil)
810 ;; Case 3: An index. If it matches one of the known indexes,
811 ;; print it as such following the contents, otherwise
812 ;; print the contents and leave the index up to the user.
813 (index
814 (concat node
815 (format
816 section-fmt
817 full-text
818 (concat contents
819 (and (member index '("cp" "fn" "ky" "pg" "tp" "vr"))
820 (concat "\n@printindex " index))))))
821 ;; Case 4: This is a deep sub-tree: export it as a list item.
822 ;; Also export as items headlines for which no section
823 ;; format has been found.
824 ((or (not section-fmt) (org-export-low-level-p headline info))
825 ;; Build the real contents of the sub-tree.
826 (concat (and (org-export-first-sibling-p headline info)
827 (format "@%s\n" (if numberedp 'enumerate 'itemize)))
828 "@item\n" full-text "\n"
829 contents
830 (if (org-export-last-sibling-p headline info)
831 (format "@end %s" (if numberedp 'enumerate 'itemize))
832 "\n")))
833 ;; Case 5: Standard headline. Export it as a section.
834 (t (concat node (format section-fmt full-text contents))))))
836 (defun org-texinfo-format-headline-default-function
837 (todo _todo-type priority text tags)
838 "Default format function for a headline.
839 See `org-texinfo-format-headline-function' for details."
840 (concat (when todo (format "@strong{%s} " todo))
841 (when priority (format "@emph{#%s} " priority))
842 text
843 (when tags (format " :%s:" (mapconcat 'identity tags ":")))))
845 ;;;; Inline Src Block
847 (defun org-texinfo-inline-src-block (inline-src-block _contents _info)
848 "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
849 CONTENTS holds the contents of the item. INFO is a plist holding
850 contextual information."
851 (let* ((code (org-element-property :value inline-src-block))
852 (separator (org-texinfo--find-verb-separator code)))
853 (concat "@verb{" separator code separator "}")))
855 ;;;; Inlinetask
857 (defun org-texinfo-inlinetask (inlinetask contents info)
858 "Transcode an INLINETASK element from Org to Texinfo.
859 CONTENTS holds the contents of the block. INFO is a plist
860 holding contextual information."
861 (let ((title (org-export-data (org-element-property :title inlinetask) info))
862 (todo (and (plist-get info :with-todo-keywords)
863 (let ((todo (org-element-property :todo-keyword inlinetask)))
864 (and todo (org-export-data todo info)))))
865 (todo-type (org-element-property :todo-type inlinetask))
866 (tags (and (plist-get info :with-tags)
867 (org-export-get-tags inlinetask info)))
868 (priority (and (plist-get info :with-priority)
869 (org-element-property :priority inlinetask))))
870 (funcall (plist-get info :texinfo-format-inlinetask-function)
871 todo todo-type priority title tags contents)))
873 (defun org-texinfo-format-inlinetask-default-function
874 (todo _todo-type priority title tags contents)
875 "Default format function for a inlinetasks.
876 See `org-texinfo-format-inlinetask-function' for details."
877 (let ((full-title
878 (concat (when todo (format "@strong{%s} " todo))
879 (when priority (format "#%c " priority))
880 title
881 (when tags (format ":%s:" (mapconcat #'identity tags ":"))))))
882 (format "@center %s\n\n%s\n" full-title contents)))
884 ;;;; Italic
886 (defun org-texinfo-italic (_italic contents info)
887 "Transcode ITALIC from Org to Texinfo.
888 CONTENTS is the text with italic markup. INFO is a plist holding
889 contextual information."
890 (org-texinfo--text-markup contents 'italic info))
892 ;;;; Item
894 (defun org-texinfo-item (item contents info)
895 "Transcode an ITEM element from Org to Texinfo.
896 CONTENTS holds the contents of the item. INFO is a plist holding
897 contextual information."
898 (format "@item%s\n%s"
899 (let ((tag (org-element-property :tag item)))
900 (if tag (concat " " (org-export-data tag info)) ""))
901 (or contents "")))
903 ;;;; Keyword
905 (defun org-texinfo-keyword (keyword _contents info)
906 "Transcode a KEYWORD element from Org to Texinfo.
907 CONTENTS is nil. INFO is a plist holding contextual information."
908 (let ((key (org-element-property :key keyword))
909 (value (org-element-property :value keyword)))
910 (cond
911 ((string= key "TEXINFO") value)
912 ((string= key "CINDEX") (format "@cindex %s" value))
913 ((string= key "FINDEX") (format "@findex %s" value))
914 ((string= key "KINDEX") (format "@kindex %s" value))
915 ((string= key "PINDEX") (format "@pindex %s" value))
916 ((string= key "TINDEX") (format "@tindex %s" value))
917 ((string= key "VINDEX") (format "@vindex %s" value))
918 ((string= key "TOC")
919 (cond ((string-match-p "\\<tables\\>" value)
920 (concat "@listoffloats "
921 (org-export-translate "Table" :utf-8 info)))
922 ((string-match-p "\\<listings\\>" value)
923 (concat "@listoffloats "
924 (org-export-translate "Listing" :utf-8 info))))))))
926 ;;;; Line Break
928 (defun org-texinfo-line-break (_line-break _contents _info)
929 "Transcode a LINE-BREAK object from Org to Texinfo.
930 CONTENTS is nil. INFO is a plist holding contextual information."
931 "@*\n")
933 ;;;; Link
935 (defun org-texinfo-link (link desc info)
936 "Transcode a LINK object from Org to Texinfo.
938 DESC is the description part of the link, or the empty string.
939 INFO is a plist holding contextual information. See
940 `org-export-data'."
941 (let* ((type (org-element-property :type link))
942 (raw-path (org-element-property :path link))
943 ;; Ensure DESC really exists, or set it to nil.
944 (desc (and (not (string= desc "")) desc))
945 (path (cond
946 ((member type '("http" "https" "ftp"))
947 (concat type ":" raw-path))
948 ((string= type "file") (org-export-file-uri raw-path))
949 (t raw-path))))
950 (cond
951 ((org-export-custom-protocol-maybe link desc 'texinfo))
952 ((org-export-inline-image-p link org-texinfo-inline-image-rules)
953 (org-texinfo--inline-image link info))
954 ((equal type "radio")
955 (let ((destination (org-export-resolve-radio-link link info)))
956 (if (not destination) desc
957 (format "@ref{%s,,%s}"
958 (org-texinfo--get-node destination info)
959 desc))))
960 ((member type '("custom-id" "id" "fuzzy"))
961 (let ((destination
962 (if (equal type "fuzzy")
963 (org-export-resolve-fuzzy-link link info)
964 (org-export-resolve-id-link link info))))
965 (pcase (org-element-type destination)
966 (`nil
967 (format org-texinfo-link-with-unknown-path-format
968 (org-texinfo--sanitize-content path)))
969 ;; Id link points to an external file.
970 (`plain-text
971 (if desc (format "@uref{file://%s,%s}" destination desc)
972 (format "@uref{file://%s}" destination)))
973 (`headline
974 (format "@ref{%s,%s}"
975 (org-texinfo--get-node destination info)
976 (cond
977 (desc)
978 ((org-export-numbered-headline-p destination info)
979 (mapconcat
980 #'number-to-string
981 (org-export-get-headline-number destination info) "."))
982 (t (org-export-data
983 (org-element-property :title destination) info)))))
985 (format "@ref{%s,,%s}"
986 (org-texinfo--get-node destination info)
987 (cond
988 (desc)
989 ;; No description is provided: first try to
990 ;; associate destination to a number.
991 ((let ((n (org-export-get-ordinal destination info)))
992 (cond ((not n) nil)
993 ((integerp n) n)
994 (t (mapconcat #'number-to-string n ".")))))
995 ;; Then grab title of headline containing
996 ;; DESTINATION.
997 ((let ((h (org-element-lineage destination '(headline) t)))
998 (and h
999 (org-export-data
1000 (org-element-property :title destination) info))))
1001 ;; Eventually, just return "Top" to refer to the
1002 ;; beginning of the info file.
1003 (t "Top")))))))
1004 ((equal type "info")
1005 (let* ((info-path (split-string path "[:#]"))
1006 (info-manual (car info-path))
1007 (info-node (or (cadr info-path) "Top"))
1008 (title (or desc "")))
1009 (format "@ref{%s,%s,,%s,}" info-node title info-manual)))
1010 ((string= type "mailto")
1011 (format "@email{%s}"
1012 (concat (org-texinfo--sanitize-content path)
1013 (and desc (concat "," desc)))))
1014 ;; External link with a description part.
1015 ((and path desc) (format "@uref{%s,%s}" path desc))
1016 ;; External link without a description part.
1017 (path (format "@uref{%s}" path))
1018 ;; No path, only description. Try to do something useful.
1020 (format (plist-get info :texinfo-link-with-unknown-path-format) desc)))))
1022 (defun org-texinfo--inline-image (link info)
1023 "Return Texinfo code for an inline image.
1024 LINK is the link pointing to the inline image. INFO is the
1025 current state of the export, as a plist."
1026 (let* ((parent (org-export-get-parent-element link))
1027 (label (and (org-element-property :name parent)
1028 (org-texinfo--get-node parent info)))
1029 (caption (org-export-get-caption parent))
1030 (shortcaption (org-export-get-caption parent t))
1031 (path (org-element-property :path link))
1032 (filename
1033 (file-name-sans-extension
1034 (if (file-name-absolute-p path) (expand-file-name path) path)))
1035 (extension (file-name-extension path))
1036 (attributes (org-export-read-attribute :attr_texinfo parent))
1037 (height (or (plist-get attributes :height) ""))
1038 (width (or (plist-get attributes :width) ""))
1039 (alt (or (plist-get attributes :alt) ""))
1040 (image (format "@image{%s,%s,%s,%s,%s}"
1041 filename width height alt extension)))
1042 (cond ((or caption shortcaption)
1043 (org-texinfo--wrap-float image
1044 info
1045 (org-export-translate "Figure" :utf-8 info)
1046 label
1047 caption
1048 shortcaption))
1049 (label (concat "@anchor{" label "}\n" image))
1050 (t image))))
1053 ;;;; Menu
1055 (defun org-texinfo-make-menu (scope info &optional master)
1056 "Create the menu for inclusion in the Texinfo document.
1058 SCOPE is a headline or a full parse tree. INFO is the
1059 communication channel, as a plist.
1061 When optional argument MASTER is non-nil, generate a master menu,
1062 including detailed node listing."
1063 (let ((menu (org-texinfo--build-menu scope info)))
1064 (when (org-string-nw-p menu)
1065 (org-element-normalize-string
1066 (format
1067 "@menu\n%s@end menu"
1068 (concat menu
1069 (when master
1070 (let ((detailmenu
1071 (org-texinfo--build-menu
1072 scope info
1073 (let ((toc-depth (plist-get info :with-toc)))
1074 (if (wholenump toc-depth) toc-depth
1075 org-texinfo-max-toc-depth)))))
1076 (when (org-string-nw-p detailmenu)
1077 (concat "\n@detailmenu\n"
1078 "--- The Detailed Node Listing ---\n\n"
1079 detailmenu
1080 "@end detailmenu\n"))))))))))
1082 (defun org-texinfo--build-menu (scope info &optional level)
1083 "Build menu for entries within SCOPE.
1084 SCOPE is a headline or a full parse tree. INFO is a plist
1085 containing contextual information. When optional argument LEVEL
1086 is an integer, build the menu recursively, down to this depth."
1087 (cond
1088 ((not level)
1089 (org-texinfo--format-entries (org-texinfo--menu-entries scope info) info))
1090 ((zerop level) nil)
1092 (org-element-normalize-string
1093 (mapconcat
1094 (lambda (h)
1095 (let ((entries (org-texinfo--menu-entries h info)))
1096 (when entries
1097 (concat
1098 (format "%s\n\n%s\n"
1099 (org-export-data (org-export-get-alt-title h info) info)
1100 (org-texinfo--format-entries entries info))
1101 (org-texinfo--build-menu h info (1- level))))))
1102 (org-texinfo--menu-entries scope info) "\n")))))
1104 (defun org-texinfo--format-entries (entries info)
1105 "Format all direct menu entries in SCOPE, as a string.
1106 SCOPE is either a headline or a full Org document. INFO is
1107 a plist containing contextual information."
1108 (org-element-normalize-string
1109 (mapconcat
1110 (lambda (h)
1111 (let* ((title (org-export-data
1112 (org-export-get-alt-title h info) info))
1113 (node (org-texinfo--get-node h info))
1114 (entry (concat "* " title ":"
1115 (if (string= title node) ":"
1116 (concat " " node ". "))))
1117 (desc (org-element-property :DESCRIPTION h)))
1118 (if (not desc) entry
1119 (format (format "%%-%ds %%s" org-texinfo-node-description-column)
1120 entry desc))))
1121 entries "\n")))
1123 (defun org-texinfo--menu-entries (scope info)
1124 "List direct children in SCOPE needing a menu entry.
1125 SCOPE is a headline or a full parse tree. INFO is a plist
1126 holding contextual information."
1127 (let* ((cache (or (plist-get info :texinfo-entries-cache)
1128 (plist-get (plist-put info :texinfo-entries-cache
1129 (make-hash-table :test #'eq))
1130 :texinfo-entries-cache)))
1131 (cached-entries (gethash scope cache 'no-cache)))
1132 (if (not (eq cached-entries 'no-cache)) cached-entries
1133 (puthash scope
1134 (org-element-map (org-element-contents scope) 'headline
1135 (lambda (h)
1136 (and (not (org-not-nil (org-element-property :COPYING h)))
1137 (not (org-element-property :footnote-section-p h))
1138 (not (org-export-low-level-p h info))
1140 info nil 'headline)
1141 cache))))
1143 ;;;; Node Property
1145 (defun org-texinfo-node-property (node-property _contents _info)
1146 "Transcode a NODE-PROPERTY element from Org to Texinfo.
1147 CONTENTS is nil. INFO is a plist holding contextual
1148 information."
1149 (format "%s:%s"
1150 (org-element-property :key node-property)
1151 (let ((value (org-element-property :value node-property)))
1152 (if value (concat " " value) ""))))
1154 ;;;; Paragraph
1156 (defun org-texinfo-paragraph (_paragraph contents _info)
1157 "Transcode a PARAGRAPH element from Org to Texinfo.
1158 CONTENTS is the contents of the paragraph, as a string. INFO is
1159 the plist used as a communication channel."
1160 contents)
1162 ;;;; Plain List
1164 (defun org-texinfo-plain-list (plain-list contents info)
1165 "Transcode a PLAIN-LIST element from Org to Texinfo.
1166 CONTENTS is the contents of the list. INFO is a plist holding
1167 contextual information."
1168 (let* ((attr (org-export-read-attribute :attr_texinfo plain-list))
1169 (indic (or (plist-get attr :indic)
1170 (plist-get info :texinfo-def-table-markup)))
1171 (table-type (plist-get attr :table-type))
1172 (type (org-element-property :type plain-list))
1173 (list-type (cond
1174 ((eq type 'ordered) "enumerate")
1175 ((eq type 'unordered) "itemize")
1176 ((member table-type '("ftable" "vtable")) table-type)
1177 (t "table"))))
1178 (format "@%s\n%s@end %s"
1179 (if (eq type 'descriptive) (concat list-type " " indic) list-type)
1180 contents
1181 list-type)))
1183 ;;;; Plain Text
1185 (defun org-texinfo-plain-text (text info)
1186 "Transcode a TEXT string from Org to Texinfo.
1187 TEXT is the string to transcode. INFO is a plist holding
1188 contextual information."
1189 ;; First protect @, { and }.
1190 (let ((output (org-texinfo--sanitize-content text)))
1191 ;; Activate smart quotes. Be sure to provide original TEXT string
1192 ;; since OUTPUT may have been modified.
1193 (when (plist-get info :with-smart-quotes)
1194 (setq output
1195 (org-export-activate-smart-quotes output :texinfo info text)))
1196 ;; LaTeX into @LaTeX{} and TeX into @TeX{}
1197 (let ((case-fold-search nil)
1198 (start 0))
1199 (while (string-match "\\(\\(?:La\\)?TeX\\)" output start)
1200 (setq output (replace-match
1201 (format "@%s{}" (match-string 1 output)) nil t output)
1202 start (match-end 0))))
1203 ;; Convert special strings.
1204 (when (plist-get info :with-special-strings)
1205 (while (string-match (regexp-quote "...") output)
1206 (setq output (replace-match "@dots{}" nil t output))))
1207 ;; Handle break preservation if required.
1208 (when (plist-get info :preserve-breaks)
1209 (setq output (replace-regexp-in-string
1210 "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" output)))
1211 ;; Return value.
1212 output))
1214 ;;;; Planning
1216 (defun org-texinfo-planning (planning _contents info)
1217 "Transcode a PLANNING element from Org to Texinfo.
1218 CONTENTS is nil. INFO is a plist holding contextual
1219 information."
1220 (concat
1221 "@noindent"
1222 (mapconcat
1223 'identity
1224 (delq nil
1225 (list
1226 (let ((closed (org-element-property :closed planning)))
1227 (when closed
1228 (concat
1229 (format "@strong{%s} " org-closed-string)
1230 (format (plist-get info :texinfo-inactive-timestamp-format)
1231 (org-timestamp-translate closed)))))
1232 (let ((deadline (org-element-property :deadline planning)))
1233 (when deadline
1234 (concat
1235 (format "@strong{%s} " org-deadline-string)
1236 (format (plist-get info :texinfo-active-timestamp-format)
1237 (org-timestamp-translate deadline)))))
1238 (let ((scheduled (org-element-property :scheduled planning)))
1239 (when scheduled
1240 (concat
1241 (format "@strong{%s} " org-scheduled-string)
1242 (format (plist-get info :texinfo-active-timestamp-format)
1243 (org-timestamp-translate scheduled)))))))
1244 " ")
1245 "@*"))
1247 ;;;; Property Drawer
1249 (defun org-texinfo-property-drawer (_property-drawer contents _info)
1250 "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
1251 CONTENTS holds the contents of the drawer. INFO is a plist
1252 holding contextual information."
1253 (and (org-string-nw-p contents)
1254 (format "@verbatim\n%s@end verbatim" contents)))
1256 ;;;; Quote Block
1258 (defun org-texinfo-quote-block (quote-block contents _info)
1259 "Transcode a QUOTE-BLOCK element from Org to Texinfo.
1260 CONTENTS holds the contents of the block. INFO is a plist
1261 holding contextual information."
1262 (let* ((title (org-element-property :name quote-block))
1263 (start-quote (concat "@quotation"
1264 (if title
1265 (format " %s" title)))))
1266 (format "%s\n%s@end quotation" start-quote contents)))
1268 ;;;; Radio Target
1270 (defun org-texinfo-radio-target (radio-target text info)
1271 "Transcode a RADIO-TARGET object from Org to Texinfo.
1272 TEXT is the text of the target. INFO is a plist holding
1273 contextual information."
1274 (format "@anchor{%s}%s"
1275 (org-export-get-reference radio-target info)
1276 text))
1278 ;;;; Section
1280 (defun org-texinfo-section (section contents info)
1281 "Transcode a SECTION element from Org to Texinfo.
1282 CONTENTS holds the contents of the section. INFO is a plist
1283 holding contextual information."
1284 (org-trim
1285 (concat contents
1286 "\n"
1287 (let ((parent (org-export-get-parent-headline section)))
1288 (and parent (org-texinfo-make-menu parent info))))))
1290 ;;;; Special Block
1292 (defun org-texinfo-special-block (special-block contents _info)
1293 "Transcode a SPECIAL-BLOCK element from Org to Texinfo.
1294 CONTENTS holds the contents of the block. INFO is a plist used
1295 as a communication channel."
1296 (let ((opt (org-export-read-attribute :attr_texinfo special-block :options))
1297 (type (org-element-property :type special-block)))
1298 (format "@%s%s\n%s@end %s"
1299 type
1300 (if opt (concat " " opt) opt)
1301 (or contents "")
1302 type)))
1304 ;;;; Src Block
1306 (defun org-texinfo-src-block (src-block _contents info)
1307 "Transcode a SRC-BLOCK element from Org to Texinfo.
1308 CONTENTS holds the contents of the item. INFO is a plist holding
1309 contextual information."
1310 (let* ((lisp (string-match-p "lisp"
1311 (org-element-property :language src-block)))
1312 (code (org-texinfo--sanitize-content
1313 (org-export-format-code-default src-block info)))
1314 (value (format
1315 (if lisp "@lisp\n%s@end lisp" "@example\n%s@end example")
1316 code))
1317 (caption (org-export-get-caption src-block))
1318 (shortcaption (org-export-get-caption src-block t)))
1319 (if (not (or caption shortcaption)) value
1320 (org-texinfo--wrap-float value
1321 info
1322 (org-export-translate "Listing" :utf-8 info)
1323 (org-export-get-reference src-block info)
1324 caption
1325 shortcaption))))
1327 ;;;; Statistics Cookie
1329 (defun org-texinfo-statistics-cookie (statistics-cookie _contents _info)
1330 "Transcode a STATISTICS-COOKIE object from Org to Texinfo.
1331 CONTENTS is nil. INFO is a plist holding contextual information."
1332 (org-element-property :value statistics-cookie))
1335 ;;;; Strike-through
1337 (defun org-texinfo-strike-through (_strike-through contents info)
1338 "Transcode STRIKE-THROUGH from Org to Texinfo.
1339 CONTENTS is the text with strike-through markup. INFO is a plist
1340 holding contextual information."
1341 (org-texinfo--text-markup contents 'strike-through info))
1343 ;;;; Subscript
1345 (defun org-texinfo-subscript (_subscript contents _info)
1346 "Transcode a SUBSCRIPT object from Org to Texinfo.
1347 CONTENTS is the contents of the object. INFO is a plist holding
1348 contextual information."
1349 (format "@math{_%s}" contents))
1351 ;;;; Superscript
1353 (defun org-texinfo-superscript (_superscript contents _info)
1354 "Transcode a SUPERSCRIPT object from Org to Texinfo.
1355 CONTENTS is the contents of the object. INFO is a plist holding
1356 contextual information."
1357 (format "@math{^%s}" contents))
1359 ;;;; Table
1361 (defun org-texinfo-table (table contents info)
1362 "Transcode a TABLE element from Org to Texinfo.
1363 CONTENTS is the contents of the table. INFO is a plist holding
1364 contextual information."
1365 (if (eq (org-element-property :type table) 'table.el)
1366 (format "@verbatim\n%s@end verbatim"
1367 (org-element-normalize-string
1368 (org-element-property :value table)))
1369 (let* ((col-width (org-export-read-attribute :attr_texinfo table :columns))
1370 (columns
1371 (if col-width (format "@columnfractions %s" col-width)
1372 (org-texinfo-table-column-widths table info)))
1373 (caption (org-export-get-caption table))
1374 (shortcaption (org-export-get-caption table t))
1375 (table-str (format "@multitable %s\n%s@end multitable"
1376 columns
1377 contents)))
1378 (if (not (or caption shortcaption)) table-str
1379 (org-texinfo--wrap-float table-str
1380 info
1381 (org-export-translate "Table" :utf-8 info)
1382 (org-export-get-reference table info)
1383 caption
1384 shortcaption)))))
1386 (defun org-texinfo-table-column-widths (table info)
1387 "Determine the largest table cell in each column to process alignment.
1388 TABLE is the table element to transcode. INFO is a plist used as
1389 a communication channel."
1390 (let ((widths (make-vector (cdr (org-export-table-dimensions table info)) 0)))
1391 (org-element-map table 'table-row
1392 (lambda (row)
1393 (let ((idx 0))
1394 (org-element-map row 'table-cell
1395 (lambda (cell)
1396 ;; Length of the cell in the original buffer is only an
1397 ;; approximation of the length of the cell in the
1398 ;; output. It can sometimes fail (e.g. it considers
1399 ;; "/a/" being larger than "ab").
1400 (let ((w (- (org-element-property :contents-end cell)
1401 (org-element-property :contents-begin cell))))
1402 (aset widths idx (max w (aref widths idx))))
1403 (cl-incf idx))
1404 info)))
1405 info)
1406 (format "{%s}" (mapconcat (lambda (w) (make-string w ?a)) widths "} {"))))
1408 ;;;; Table Cell
1410 (defun org-texinfo-table-cell (table-cell contents info)
1411 "Transcode a TABLE-CELL element from Org to Texinfo.
1412 CONTENTS is the cell contents. INFO is a plist used as
1413 a communication channel."
1414 (concat
1415 (let ((scientific-notation
1416 (plist-get info :texinfo-table-scientific-notation)))
1417 (if (and contents
1418 scientific-notation
1419 (string-match orgtbl-exp-regexp contents))
1420 ;; Use appropriate format string for scientific notation.
1421 (format scientific-notation
1422 (match-string 1 contents)
1423 (match-string 2 contents))
1424 contents))
1425 (when (org-export-get-next-element table-cell info) "\n@tab ")))
1427 ;;;; Table Row
1429 (defun org-texinfo-table-row (table-row contents info)
1430 "Transcode a TABLE-ROW element from Org to Texinfo.
1431 CONTENTS is the contents of the row. INFO is a plist used as
1432 a communication channel."
1433 ;; Rules are ignored since table separators are deduced from
1434 ;; borders of the current row.
1435 (when (eq (org-element-property :type table-row) 'standard)
1436 (let ((rowgroup-tag
1437 (if (and (= 1 (org-export-table-row-group table-row info))
1438 (org-export-table-has-header-p
1439 (org-export-get-parent-table table-row) info))
1440 "@headitem "
1441 "@item ")))
1442 (concat rowgroup-tag contents "\n"))))
1444 ;;;; Target
1446 (defun org-texinfo-target (target _contents info)
1447 "Transcode a TARGET object from Org to Texinfo.
1448 CONTENTS is nil. INFO is a plist holding contextual
1449 information."
1450 (format "@anchor{%s}" (org-export-get-reference target info)))
1452 ;;;; Timestamp
1454 (defun org-texinfo-timestamp (timestamp _contents info)
1455 "Transcode a TIMESTAMP object from Org to Texinfo.
1456 CONTENTS is nil. INFO is a plist holding contextual
1457 information."
1458 (let ((value (org-texinfo-plain-text
1459 (org-timestamp-translate timestamp) info)))
1460 (pcase (org-element-property :type timestamp)
1461 ((or `active `active-range)
1462 (format (plist-get info :texinfo-active-timestamp-format) value))
1463 ((or `inactive `inactive-range)
1464 (format (plist-get info :texinfo-inactive-timestamp-format) value))
1465 (_ (format (plist-get info :texinfo-diary-timestamp-format) value)))))
1467 ;;;; Underline
1469 (defun org-texinfo-underline (_underline contents info)
1470 "Transcode UNDERLINE from Org to Texinfo.
1471 CONTENTS is the text with underline markup. INFO is a plist
1472 holding contextual information."
1473 (org-texinfo--text-markup contents 'underline info))
1475 ;;;; Verbatim
1477 (defun org-texinfo-verbatim (verbatim _contents info)
1478 "Transcode a VERBATIM object from Org to Texinfo.
1479 CONTENTS is nil. INFO is a plist used as a communication
1480 channel."
1481 (org-texinfo--text-markup
1482 (org-element-property :value verbatim) 'verbatim info))
1484 ;;;; Verse Block
1486 (defun org-texinfo-verse-block (_verse-block contents _info)
1487 "Transcode a VERSE-BLOCK element from Org to Texinfo.
1488 CONTENTS is verse block contents. INFO is a plist holding
1489 contextual information."
1490 (format "@display\n%s@end display" contents))
1493 ;;; Interactive functions
1495 (defun org-texinfo-export-to-texinfo
1496 (&optional async subtreep visible-only body-only ext-plist)
1497 "Export current buffer to a Texinfo file.
1499 If narrowing is active in the current buffer, only export its
1500 narrowed part.
1502 If a region is active, export that region.
1504 A non-nil optional argument ASYNC means the process should happen
1505 asynchronously. The resulting file should be accessible through
1506 the `org-export-stack' interface.
1508 When optional argument SUBTREEP is non-nil, export the sub-tree
1509 at point, extracting information from the headline properties
1510 first.
1512 When optional argument VISIBLE-ONLY is non-nil, don't export
1513 contents of hidden elements.
1515 When optional argument BODY-ONLY is non-nil, only write code
1516 between \"\\begin{document}\" and \"\\end{document}\".
1518 EXT-PLIST, when provided, is a property list with external
1519 parameters overriding Org default settings, but still inferior to
1520 file-local settings.
1522 Return output file's name."
1523 (interactive)
1524 (let ((outfile (org-export-output-file-name ".texi" subtreep))
1525 (org-export-coding-system org-texinfo-coding-system))
1526 (org-export-to-file 'texinfo outfile
1527 async subtreep visible-only body-only ext-plist)))
1529 (defun org-texinfo-export-to-info
1530 (&optional async subtreep visible-only body-only ext-plist)
1531 "Export current buffer to Texinfo then process through to INFO.
1533 If narrowing is active in the current buffer, only export its
1534 narrowed part.
1536 If a region is active, export that region.
1538 A non-nil optional argument ASYNC means the process should happen
1539 asynchronously. The resulting file should be accessible through
1540 the `org-export-stack' interface.
1542 When optional argument SUBTREEP is non-nil, export the sub-tree
1543 at point, extracting information from the headline properties
1544 first.
1546 When optional argument VISIBLE-ONLY is non-nil, don't export
1547 contents of hidden elements.
1549 When optional argument BODY-ONLY is non-nil, only write code
1550 between \"\\begin{document}\" and \"\\end{document}\".
1552 EXT-PLIST, when provided, is a property list with external
1553 parameters overriding Org default settings, but still inferior to
1554 file-local settings.
1556 When optional argument PUB-DIR is set, use it as the publishing
1557 directory.
1559 Return INFO file's name."
1560 (interactive)
1561 (let ((outfile (org-export-output-file-name ".texi" subtreep))
1562 (org-export-coding-system org-texinfo-coding-system))
1563 (org-export-to-file 'texinfo outfile
1564 async subtreep visible-only body-only ext-plist
1565 (lambda (file) (org-texinfo-compile file)))))
1567 ;;;###autoload
1568 (defun org-texinfo-publish-to-texinfo (plist filename pub-dir)
1569 "Publish an org file to Texinfo.
1571 FILENAME is the filename of the Org file to be published. PLIST
1572 is the property list for the given project. PUB-DIR is the
1573 publishing directory.
1575 Return output file name."
1576 (org-publish-org-to 'texinfo filename ".texi" plist pub-dir))
1578 ;;;###autoload
1579 (defun org-texinfo-convert-region-to-texinfo ()
1580 "Assume the current region has Org syntax, and convert it to Texinfo.
1581 This can be used in any buffer. For example, you can write an
1582 itemized list in Org syntax in an Texinfo buffer and use this
1583 command to convert it."
1584 (interactive)
1585 (org-export-replace-region-by 'texinfo))
1587 (defun org-texinfo-compile (file)
1588 "Compile a texinfo file.
1590 FILE is the name of the file being compiled. Processing is done
1591 through the command specified in `org-texinfo-info-process',
1592 which see. Output is redirected to \"*Org INFO Texinfo Output*\"
1593 buffer.
1595 Return INFO file name or an error if it couldn't be produced."
1596 (message "Processing Texinfo file %s..." file)
1597 (let* ((log-name "*Org INFO Texinfo Output*")
1598 (log (get-buffer-create log-name))
1599 (output
1600 (org-compile-file file org-texinfo-info-process "info"
1601 (format "See %S for details" log-name)
1602 log)))
1603 (when org-texinfo-remove-logfiles
1604 (let ((base (file-name-sans-extension output)))
1605 (dolist (ext org-texinfo-logfiles-extensions)
1606 (let ((file (concat base "." ext)))
1607 (when (file-exists-p file) (delete-file file))))))
1608 (message "Process completed.")
1609 output))
1612 (provide 'ox-texinfo)
1614 ;; Local variables:
1615 ;; generated-autoload-file: "org-loaddefs.el"
1616 ;; End:
1618 ;;; ox-texinfo.el ends here