Merge branch 'maint'
[org-mode.git] / lisp / ox-texinfo.el
blob9b273494c82fd0b6567f4137f6d09f48943fb74a
1 ;;; ox-texinfo.el --- Texinfo Back-End for Org Export Engine -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2012-2017 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 <https://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 (:filter-final-output . org-texinfo--untabify))
88 :menu-entry
89 '(?i "Export to Texinfo"
90 ((?t "As TEXI file" org-texinfo-export-to-texinfo)
91 (?i "As INFO file" org-texinfo-export-to-info)
92 (?o "As INFO file and open"
93 (lambda (a s v b)
94 (if a (org-texinfo-export-to-info t s v b)
95 (org-open-file (org-texinfo-export-to-info nil s v b)))))))
96 :options-alist
97 '((:texinfo-filename "TEXINFO_FILENAME" nil nil t)
98 (:texinfo-class "TEXINFO_CLASS" nil org-texinfo-default-class t)
99 (:texinfo-header "TEXINFO_HEADER" nil nil newline)
100 (:texinfo-post-header "TEXINFO_POST_HEADER" nil nil newline)
101 (:subtitle "SUBTITLE" nil nil parse)
102 (:subauthor "SUBAUTHOR" nil nil newline)
103 (:texinfo-dircat "TEXINFO_DIR_CATEGORY" nil nil t)
104 (:texinfo-dirtitle "TEXINFO_DIR_TITLE" nil nil t)
105 (:texinfo-dirdesc "TEXINFO_DIR_DESC" nil nil t)
106 (:texinfo-printed-title "TEXINFO_PRINTED_TITLE" nil nil t)
107 ;; Other variables.
108 (:texinfo-classes nil nil org-texinfo-classes)
109 (:texinfo-format-headline-function nil nil org-texinfo-format-headline-function)
110 (:texinfo-node-description-column nil nil org-texinfo-node-description-column)
111 (:texinfo-active-timestamp-format nil nil org-texinfo-active-timestamp-format)
112 (:texinfo-inactive-timestamp-format nil nil org-texinfo-inactive-timestamp-format)
113 (:texinfo-diary-timestamp-format nil nil org-texinfo-diary-timestamp-format)
114 (:texinfo-link-with-unknown-path-format nil nil org-texinfo-link-with-unknown-path-format)
115 (:texinfo-tables-verbatim nil nil org-texinfo-tables-verbatim)
116 (:texinfo-table-scientific-notation nil nil org-texinfo-table-scientific-notation)
117 (:texinfo-table-default-markup nil nil org-texinfo-table-default-markup)
118 (:texinfo-text-markup-alist nil nil org-texinfo-text-markup-alist)
119 (:texinfo-format-drawer-function nil nil org-texinfo-format-drawer-function)
120 (:texinfo-format-inlinetask-function nil nil org-texinfo-format-inlinetask-function)))
124 ;;; User Configurable Variables
126 (defgroup org-export-texinfo nil
127 "Options for exporting Org mode files to Texinfo."
128 :tag "Org Export Texinfo"
129 :version "24.4"
130 :package-version '(Org . "8.0")
131 :group 'org-export)
133 ;;;; Preamble
135 (defcustom org-texinfo-coding-system nil
136 "Default document encoding for Texinfo output.
138 If nil it will default to `buffer-file-coding-system'."
139 :group 'org-export-texinfo
140 :type 'coding-system)
142 (defcustom org-texinfo-default-class "info"
143 "The default Texinfo class."
144 :group 'org-export-texinfo
145 :type '(string :tag "Texinfo class"))
147 (defcustom org-texinfo-classes
148 '(("info"
149 "@documentencoding AUTO\n@documentlanguage AUTO"
150 ("@chapter %s" "@unnumbered %s" "@chapheading %s" "@appendix %s")
151 ("@section %s" "@unnumberedsec %s" "@heading %s" "@appendixsec %s")
152 ("@subsection %s" "@unnumberedsubsec %s" "@subheading %s"
153 "@appendixsubsec %s")
154 ("@subsubsection %s" "@unnumberedsubsubsec %s" "@subsubheading %s"
155 "@appendixsubsubsec %s")))
156 "Alist of Texinfo classes and associated header and structure.
157 If #+TEXINFO_CLASS is set in the buffer, use its value and the
158 associated information. Here is the structure of a class
159 definition:
161 (class-name
162 header-string
163 (numbered-1 unnumbered-1 unnumbered-no-toc-1 appendix-1)
164 (numbered-2 unnumbered-2 unnumbered-no-toc-2 appendix-2)
165 ...)
168 The header string
169 -----------------
171 The header string is inserted in the header of the generated
172 document, right after \"@setfilename\" and \"@settitle\"
173 commands.
175 If it contains the special string
177 \"@documentencoding AUTO\"
179 \"AUTO\" will be replaced with an appropriate coding system. See
180 `org-texinfo-coding-system' for more information. Likewise, if
181 the string contains the special string
183 \"@documentlanguage AUTO\"
185 \"AUTO\" will be replaced with the language defined in the
186 buffer, through #+LANGUAGE keyword, or globally, with
187 `org-export-default-language', which see.
190 The sectioning structure
191 ------------------------
193 The sectioning structure of the class is given by the elements
194 following the header string. For each sectioning level, a number
195 of strings is specified. A %s formatter is mandatory in each
196 section string and will be replaced by the title of the section."
197 :group 'org-export-texinfo
198 :version "27.1"
199 :package-version '(Org . "9.2")
200 :type '(repeat
201 (list (string :tag "Texinfo class")
202 (string :tag "Texinfo header")
203 (repeat :tag "Levels" :inline t
204 (choice
205 (list :tag "Heading"
206 (string :tag " numbered")
207 (string :tag " unnumbered")
208 (string :tag "unnumbered-no-toc")
209 (string :tag " appendix")))))))
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 "26.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.
273 The format should have \"%s\" twice, for mantissa and exponent
274 \(i.e. \"%s\\\\times10^{%s}\").
276 When nil, no transformation is made."
277 :group 'org-export-texinfo
278 :type '(choice
279 (string :tag "Format string")
280 (const :tag "No formatting" nil)))
282 (defcustom org-texinfo-table-default-markup "@asis"
283 "Default markup for first column in two-column tables.
285 This should an indicating command, e.g., \"@code\", \"@kbd\" or
286 \"@samp\".
288 It can be overridden locally using the \":indic\" attribute."
289 :group 'org-export-texinfo
290 :type 'string
291 :version "26.1"
292 :package-version '(Org . "9.1")
293 :safe #'stringp)
295 ;;;; Text markup
297 (defcustom org-texinfo-text-markup-alist '((bold . "@strong{%s}")
298 (code . code)
299 (italic . "@emph{%s}")
300 (verbatim . samp))
301 "Alist of Texinfo expressions to convert text markup.
303 The key must be a symbol among `bold', `code', `italic',
304 `strike-through', `underscore' and `verbatim'. The value is
305 a formatting string to wrap fontified text with.
307 Value can also be set to the following symbols: `verb', `samp'
308 and `code'. With the first one, Org uses \"@verb\" to create
309 a format string and selects a delimiter character that isn't in
310 the string. For the other two, Org uses \"@samp\" or \"@code\"
311 to typeset and protects special characters.
313 When no association is found for a given markup, text is returned
314 as-is."
315 :group 'org-export-texinfo
316 :version "26.1"
317 :package-version '(Org . "9.1")
318 :type 'alist
319 :options '(bold code italic strike-through underscore verbatim))
321 ;;;; Drawers
323 (defcustom org-texinfo-format-drawer-function (lambda (_name contents) contents)
324 "Function called to format a drawer in Texinfo code.
326 The function must accept two parameters:
327 NAME the drawer name, like \"LOGBOOK\"
328 CONTENTS the contents of the drawer.
330 The function should return the string to be exported.
332 The default function simply returns the value of CONTENTS."
333 :group 'org-export-texinfo
334 :version "24.4"
335 :package-version '(Org . "8.2")
336 :type 'function)
338 ;;;; Inlinetasks
340 (defcustom org-texinfo-format-inlinetask-function
341 'org-texinfo-format-inlinetask-default-function
342 "Function called to format an inlinetask in Texinfo code.
344 The function must accept six parameters:
345 TODO the todo keyword, as a string
346 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
347 PRIORITY the inlinetask priority, as a string
348 NAME the inlinetask name, as a string.
349 TAGS the inlinetask tags, as a list of strings.
350 CONTENTS the contents of the inlinetask, as a string.
352 The function should return the string to be exported."
353 :group 'org-export-texinfo
354 :type 'function)
356 ;;;; Compilation
358 (defcustom org-texinfo-info-process '("makeinfo --no-split %f")
359 "Commands to process a Texinfo file to an INFO file.
361 This is a list of strings, each of them will be given to the
362 shell as a command. %f in the command will be replaced by the
363 relative file name, %F by the absolute file name, %b by the file
364 base name (i.e. without directory and extension parts), %o by the
365 base directory of the file and %O by the absolute file name of
366 the output file."
367 :group 'org-export-texinfo
368 :version "26.1"
369 :package-version '(Org . "9.1")
370 :type '(repeat :tag "Shell command sequence"
371 (string :tag "Shell command")))
373 (defcustom org-texinfo-logfiles-extensions
374 '("aux" "toc" "cp" "fn" "ky" "pg" "tp" "vr")
375 "The list of file extensions to consider as Texinfo logfiles.
376 The logfiles will be remove if `org-texinfo-remove-logfiles' is
377 non-nil."
378 :group 'org-export-texinfo
379 :type '(repeat (string :tag "Extension")))
381 (defcustom org-texinfo-remove-logfiles t
382 "Non-nil means remove the logfiles produced by compiling a Texinfo file.
383 By default, logfiles are files with these extensions: .aux, .toc,
384 .cp, .fn, .ky, .pg and .tp. To define the set of logfiles to remove,
385 set `org-texinfo-logfiles-extensions'."
386 :group 'org-export-latex
387 :type 'boolean)
389 ;;; Constants
391 (defconst org-texinfo-max-toc-depth 4
392 "Maximum depth for creation of detailed menu listings.
393 Beyond this depth, Texinfo will not recognize the nodes and will
394 cause errors. Left as a constant in case this value ever
395 changes.")
397 (defconst org-texinfo-supported-coding-systems
398 '("US-ASCII" "UTF-8" "ISO-8859-15" "ISO-8859-1" "ISO-8859-2" "koi8-r" "koi8-u")
399 "List of coding systems supported by Texinfo, as strings.
400 Specified coding system will be matched against these strings.
401 If two strings share the same prefix (e.g. \"ISO-8859-1\" and
402 \"ISO-8859-15\"), the most specific one has to be listed first.")
404 (defconst org-texinfo-inline-image-rules
405 (list (cons "file"
406 (regexp-opt '("eps" "pdf" "png" "jpg" "jpeg" "gif" "svg"))))
407 "Rules characterizing image files that can be inlined.")
410 ;;; Internal Functions
412 (defun org-texinfo--untabify (s _backend _info)
413 "Remove TAB characters in string S."
414 (replace-regexp-in-string "\t" (make-string tab-width ?\s) s))
416 (defun org-texinfo--filter-section-blank-lines (headline _backend _info)
417 "Filter controlling number of blank lines after a section."
418 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" "\n\n" headline))
420 (defun org-texinfo--normalize-headlines (tree _backend info)
421 "Normalize headlines in TREE.
423 BACK-END is the symbol specifying back-end used for export. INFO
424 is a plist used as a communication channel.
426 Make sure every headline in TREE contains a section, since those
427 are required to install a menu. Also put exactly one blank line
428 at the end of each section.
430 Return new tree."
431 (org-element-map tree 'headline
432 (lambda (hl)
433 (org-element-put-property hl :post-blank 1)
434 (let ((contents (org-element-contents hl)))
435 (when contents
436 (let ((first (org-element-map contents '(headline section)
437 #'identity info t)))
438 (unless (eq (org-element-type first) 'section)
439 (apply #'org-element-set-contents
441 (cons `(section (:parent ,hl)) contents)))))))
442 info)
443 tree)
445 (defun org-texinfo--find-verb-separator (s)
446 "Return a character not used in string S.
447 This is used to choose a separator for constructs like \\verb."
448 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
449 (cl-loop for c across ll
450 when (not (string-match (regexp-quote (char-to-string c)) s))
451 return (char-to-string c))))
453 (defun org-texinfo--text-markup (text markup _info)
454 "Format TEXT depending on MARKUP text markup.
455 INFO is a plist used as a communication channel. See
456 `org-texinfo-text-markup-alist' for details."
457 (pcase (cdr (assq markup org-texinfo-text-markup-alist))
458 (`nil text) ;no markup: return raw text
459 (`code (format "@code{%s}" (org-texinfo--sanitize-content text)))
460 (`samp (format "@samp{%s}" (org-texinfo--sanitize-content text)))
461 (`verb
462 (let ((separator (org-texinfo--find-verb-separator text)))
463 (format "@verb{%s%s%s}" separator text separator)))
464 ;; Else use format string.
465 (fmt (format fmt text))))
467 (defun org-texinfo--get-node (datum info)
468 "Return node or anchor associated to DATUM.
469 DATUM is a headline, a radio-target or a target. INFO is a plist
470 used as a communication channel. The function guarantees the
471 node or anchor name is unique."
472 (let ((cache (plist-get info :texinfo-node-cache)))
473 (or (cdr (assq datum cache))
474 (let* ((salt 0)
475 (basename
476 (org-texinfo--sanitize-node
477 (pcase (org-element-type datum)
478 (`headline
479 (org-texinfo--sanitize-title
480 (org-export-get-alt-title datum info) info))
481 (`radio-target
482 (org-texinfo--sanitize-title
483 (org-element-contents datum) info))
484 (`target
485 (org-export-data (org-element-property :value datum) info))
486 (type
487 (error "Cannot generate node name for type: %S" type)))))
488 (name basename))
489 ;; Org exports deeper elements before their parents. If two
490 ;; node names collide -- e.g., they have the same title --
491 ;; within the same hierarchy, the second one would get the
492 ;; shorter node name. This is counter-intuitive.
493 ;; Consequently, we ensure that every parent headline get
494 ;; its node beforehand. As a recursive operation, this
495 ;; achieves the desired effect.
496 (let ((parent (org-element-lineage datum '(headline))))
497 (when (and parent (not (assq parent cache)))
498 (org-texinfo--get-node parent info)
499 (setq cache (plist-get info :texinfo-node-cache))))
500 ;; Ensure NAME is unique and not reserved node name "Top".
501 (while (or (equal name "Top") (rassoc name cache))
502 (setq name (concat basename (format " (%d)" (cl-incf salt)))))
503 (plist-put info :texinfo-node-cache (cons (cons datum name) cache))
504 name))))
506 (defun org-texinfo--sanitize-node (title)
507 "Bend string TITLE to node line requirements.
508 Trim string and collapse multiple whitespace characters as they
509 are not significant. Replace leading left parenthesis, when
510 followed by a right parenthesis, with a square bracket. Remove
511 periods, commas and colons."
512 (org-trim
513 (replace-regexp-in-string
514 "[ \t]+" " "
515 (replace-regexp-in-string
516 "[:,.]" ""
517 (replace-regexp-in-string "\\`(\\(.*?)\\)" "[\\1" title)))))
519 (defun org-texinfo--sanitize-title (title info)
520 "Make TITLE suitable as a section name.
521 TITLE is a string or a secondary string. INFO is the current
522 export state, as a plist."
523 (org-export-data-with-backend
524 title (org-export-toc-entry-backend 'texinfo) info))
526 (defun org-texinfo--sanitize-content (text)
527 "Escape special characters in string TEXT.
528 Special characters are: @ { }"
529 (replace-regexp-in-string "[@{}]" "@\\&" text))
531 (defun org-texinfo--wrap-float (value info &optional type label caption short)
532 "Wrap string VALUE within a @float command.
533 INFO is the current export state, as a plist. TYPE is float
534 type, as a string. LABEL is the cross reference label for the
535 float, as a string. CAPTION and SHORT are, respectively, the
536 caption and shortcaption used for the float, as secondary
537 strings (e.g., returned by `org-export-get-caption')."
538 (let* ((backend
539 (org-export-toc-entry-backend 'texinfo
540 (cons 'footnote-reference
541 (lambda (f c i) (org-export-with-backend 'texinfo f c i)))))
542 (short-backend
543 (org-export-toc-entry-backend 'texinfo
544 '(inline-src-block . ignore)
545 '(verbatim . ignore)))
546 (short-str
547 (if (and short caption)
548 (format "@shortcaption{%s}\n"
549 (org-export-data-with-backend short short-backend info))
550 ""))
551 (caption-str
552 (if (or short caption)
553 (format "@caption{%s}\n"
554 (org-export-data-with-backend
555 (or caption short)
556 (if (equal short-str "") short-backend backend)
557 info))
558 "")))
559 (format "@float %s%s\n%s\n%s%s@end float"
560 type (if label (concat "," label) "") value caption-str short-str)))
562 ;;; Template
564 (defun org-texinfo-template (contents info)
565 "Return complete document string after Texinfo conversion.
566 CONTENTS is the transcoded contents string. INFO is a plist
567 holding export options."
568 (let ((title (org-export-data (plist-get info :title) info))
569 ;; Copying data is the contents of the first headline in
570 ;; parse tree with a non-nil copying property.
571 (copying (org-element-map (plist-get info :parse-tree) 'headline
572 (lambda (hl)
573 (and (org-not-nil (org-element-property :COPYING hl))
574 (org-element-contents hl)))
575 info t)))
576 (concat
577 "\\input texinfo @c -*- texinfo -*-\n"
578 "@c %**start of header\n"
579 (let ((file (or (plist-get info :texinfo-filename)
580 (let ((f (plist-get info :output-file)))
581 (and f (concat (file-name-sans-extension f) ".info"))))))
582 (and file (format "@setfilename %s\n" file)))
583 (format "@settitle %s\n" title)
584 ;; Insert class-defined header.
585 (org-element-normalize-string
586 (let ((header (nth 1 (assoc (plist-get info :texinfo-class)
587 org-texinfo-classes)))
588 (coding
589 (catch 'coding-system
590 (let ((case-fold-search t)
591 (name (symbol-name (or org-texinfo-coding-system
592 buffer-file-coding-system))))
593 (dolist (system org-texinfo-supported-coding-systems "UTF-8")
594 (when (string-match-p (regexp-quote system) name)
595 (throw 'coding-system system))))))
596 (language (plist-get info :language))
597 (case-fold-search nil))
598 ;; Auto coding system.
599 (replace-regexp-in-string
600 "^@documentencoding \\(AUTO\\)$"
601 coding
602 (replace-regexp-in-string
603 "^@documentlanguage \\(AUTO\\)$" language header t nil 1) t nil 1)))
604 ;; Additional header options set by #+TEXINFO_HEADER.
605 (let ((texinfo-header (plist-get info :texinfo-header)))
606 (and texinfo-header (org-element-normalize-string texinfo-header)))
607 "@c %**end of header\n\n"
608 ;; Additional options set by #+TEXINFO_POST_HEADER.
609 (let ((texinfo-post-header (plist-get info :texinfo-post-header)))
610 (and texinfo-post-header
611 (org-element-normalize-string texinfo-post-header)))
612 ;; Copying.
613 (and copying
614 (format "@copying\n%s@end copying\n\n"
615 (org-element-normalize-string
616 (org-export-data copying info))))
617 ;; Info directory information. Only supply if both title and
618 ;; category are provided.
619 (let ((dircat (plist-get info :texinfo-dircat))
620 (dirtitle
621 (let ((title (plist-get info :texinfo-dirtitle)))
622 (and title
623 (string-match "^\\(?:\\* \\)?\\(.*?\\)\\(\\.\\)?$" title)
624 (format "* %s." (match-string 1 title))))))
625 (when (and dircat dirtitle)
626 (concat "@dircategory " dircat "\n"
627 "@direntry\n"
628 (let ((dirdesc
629 (let ((desc (plist-get info :texinfo-dirdesc)))
630 (cond ((not desc) nil)
631 ((string-suffix-p "." desc) desc)
632 (t (concat desc "."))))))
633 (if dirdesc (format "%-23s %s" dirtitle dirdesc) dirtitle))
634 "\n"
635 "@end direntry\n\n")))
636 ;; Title
637 "@finalout\n"
638 "@titlepage\n"
639 (when (plist-get info :with-title)
640 (concat
641 (format "@title %s\n"
642 (or (plist-get info :texinfo-printed-title) title ""))
643 (let ((subtitle (plist-get info :subtitle)))
644 (when subtitle
645 (format "@subtitle %s\n"
646 (org-export-data subtitle info))))))
647 (when (plist-get info :with-author)
648 (concat
649 ;; Primary author.
650 (let ((author (org-string-nw-p
651 (org-export-data (plist-get info :author) info)))
652 (email (and (plist-get info :with-email)
653 (org-string-nw-p
654 (org-export-data (plist-get info :email) info)))))
655 (cond ((and author email)
656 (format "@author %s (@email{%s})\n" author email))
657 (author (format "@author %s\n" author))
658 (email (format "@author @email{%s}\n" email))))
659 ;; Other authors.
660 (let ((subauthor (plist-get info :subauthor)))
661 (and subauthor
662 (org-element-normalize-string
663 (replace-regexp-in-string "^" "@author " subauthor))))))
664 (and copying "@page\n@vskip 0pt plus 1filll\n@insertcopying\n")
665 "@end titlepage\n\n"
666 ;; Table of contents.
667 (and (plist-get info :with-toc) "@contents\n\n")
668 ;; Configure Top Node when not for TeX. Also include contents
669 ;; from the first section of the document.
670 "@ifnottex\n"
671 "@node Top\n"
672 (format "@top %s\n" title)
673 (let* ((first-section
674 (org-element-map (plist-get info :parse-tree) 'section
675 #'identity info t '(headline)))
676 (top-contents
677 (org-export-data (org-element-contents first-section) info)))
678 (and (org-string-nw-p top-contents) (concat "\n" top-contents)))
679 "@end ifnottex\n\n"
680 ;; Menu.
681 (org-texinfo-make-menu (plist-get info :parse-tree) info 'master)
682 "\n"
683 ;; Document's body.
684 contents "\n"
685 ;; Creator.
686 (and (plist-get info :with-creator)
687 (concat (plist-get info :creator) "\n"))
688 ;; Document end.
689 "@bye")))
693 ;;; Transcode Functions
695 ;;;; Bold
697 (defun org-texinfo-bold (_bold contents info)
698 "Transcode BOLD from Org to Texinfo.
699 CONTENTS is the text with bold markup. INFO is a plist holding
700 contextual information."
701 (org-texinfo--text-markup contents 'bold info))
703 ;;;; Center Block
705 (defun org-texinfo-center-block (_center-block contents _info)
706 "Transcode a CENTER-BLOCK element from Org to Texinfo.
707 CONTENTS holds the contents of the block. INFO is a plist used
708 as a communication channel."
709 contents)
711 ;;;; Clock
713 (defun org-texinfo-clock (clock _contents info)
714 "Transcode a CLOCK element from Org to Texinfo.
715 CONTENTS is nil. INFO is a plist holding contextual
716 information."
717 (concat
718 "@noindent"
719 (format "@strong{%s} " org-clock-string)
720 (format (plist-get info :texinfo-inactive-timestamp-format)
721 (concat (org-timestamp-translate (org-element-property :value clock))
722 (let ((time (org-element-property :duration clock)))
723 (and time (format " (%s)" time)))))
724 "@*"))
726 ;;;; Code
728 (defun org-texinfo-code (code _contents info)
729 "Transcode a CODE object from Org to Texinfo.
730 CONTENTS is nil. INFO is a plist used as a communication
731 channel."
732 (org-texinfo--text-markup (org-element-property :value code) 'code info))
734 ;;;; Drawer
736 (defun org-texinfo-drawer (drawer contents info)
737 "Transcode a DRAWER element from Org to Texinfo.
738 CONTENTS holds the contents of the block. INFO is a plist
739 holding contextual information."
740 (let* ((name (org-element-property :drawer-name drawer))
741 (output (funcall (plist-get info :texinfo-format-drawer-function)
742 name contents)))
743 output))
745 ;;;; Dynamic Block
747 (defun org-texinfo-dynamic-block (_dynamic-block contents _info)
748 "Transcode a DYNAMIC-BLOCK element from Org to Texinfo.
749 CONTENTS holds the contents of the block. INFO is a plist
750 holding contextual information."
751 contents)
753 ;;;; Entity
755 (defun org-texinfo-entity (entity _contents _info)
756 "Transcode an ENTITY object from Org to Texinfo."
757 ;; Since there is not specific Texinfo entry in entities, use
758 ;; Texinfo-specific commands whenever possible, and fallback to
759 ;; UTF-8 otherwise.
760 (pcase (org-element-property :name entity)
761 ("AElig" "@AE{}")
762 ("aelig" "@ae{}")
763 ((or "bull" "bullet") "@bullet{}")
764 ("copy" "@copyright{}")
765 ("deg" "@textdegree{}")
766 ((or "dots" "hellip") "@dots{}")
767 ("equiv" "@equiv{}")
768 ((or "euro" "EUR") "@euro{}")
769 ((or "ge" "geq") "@geq{}")
770 ("laquo" "@guillemetleft{}")
771 ("iexcl" "@exclamdown{}")
772 ("imath" "@dotless{i}")
773 ("iquest" "@questiondown{}")
774 ("jmath" "@dotless{j}")
775 ((or "le" "leq") "@leq{}")
776 ("lsaquo" "@guilsinglleft{}")
777 ("mdash" "---")
778 ("minus" "@minus{}")
779 ("nbsp" "@tie{}")
780 ("ndash" "--")
781 ("OElig" "@OE{}")
782 ("oelig" "@oe{}")
783 ("ordf" "@ordf{}")
784 ("ordm" "@ordm{}")
785 ("pound" "@pound{}")
786 ("raquo" "@guillemetright{}")
787 ((or "rArr" "Rightarrow") "@result{}")
788 ("reg" "@registeredsymbol{}")
789 ((or "rightarrow" "to" "rarr") "@arrow{}")
790 ("rsaquo" "@guilsinglright{}")
791 ("thorn" "@th{}")
792 ("THORN" "@TH{}")
793 ((and (pred (string-prefix-p "_")) name) ;spacing entities
794 (format "@w{%s}" (substring name 1)))
795 (_ (org-element-property :utf-8 entity))))
797 ;;;; Example Block
799 (defun org-texinfo-example-block (example-block _contents info)
800 "Transcode an EXAMPLE-BLOCK element from Org to Texinfo.
801 CONTENTS is nil. INFO is a plist holding contextual
802 information."
803 (format "@example\n%s@end example"
804 (org-texinfo--sanitize-content
805 (org-export-format-code-default example-block info))))
807 ;;; Export Block
809 (defun org-texinfo-export-block (export-block _contents _info)
810 "Transcode a EXPORT-BLOCK element from Org to Texinfo.
811 CONTENTS is nil. INFO is a plist holding contextual information."
812 (when (string= (org-element-property :type export-block) "TEXINFO")
813 (org-remove-indentation (org-element-property :value export-block))))
815 ;;; Export Snippet
817 (defun org-texinfo-export-snippet (export-snippet _contents _info)
818 "Transcode a EXPORT-SNIPPET object from Org to Texinfo.
819 CONTENTS is nil. INFO is a plist holding contextual information."
820 (when (eq (org-export-snippet-backend export-snippet) 'texinfo)
821 (org-element-property :value export-snippet)))
823 ;;;; Fixed Width
825 (defun org-texinfo-fixed-width (fixed-width _contents _info)
826 "Transcode a FIXED-WIDTH element from Org to Texinfo.
827 CONTENTS is nil. INFO is a plist holding contextual information."
828 (format "@example\n%s@end example"
829 (org-remove-indentation
830 (org-texinfo--sanitize-content
831 (org-element-property :value fixed-width)))))
833 ;;;; Footnote Reference
835 (defun org-texinfo-footnote-reference (footnote _contents info)
836 "Create a footnote reference for FOOTNOTE.
838 FOOTNOTE is the footnote to define. CONTENTS is nil. INFO is a
839 plist holding contextual information."
840 (let ((def (org-export-get-footnote-definition footnote info)))
841 (format "@footnote{%s}"
842 (org-trim (org-export-data def info)))))
844 ;;;; Headline
846 (defun org-texinfo-headline (headline contents info)
847 "Transcode a HEADLINE element from Org to Texinfo.
848 CONTENTS holds the contents of the headline. INFO is a plist
849 holding contextual information."
850 (cond
851 ((org-element-property :footnote-section-p headline) nil)
852 ((org-not-nil (org-export-get-node-property :COPYING headline t)) nil)
854 (let* ((index (let ((i (org-export-get-node-property :INDEX headline t)))
855 (and (member i '("cp" "fn" "ky" "pg" "tp" "vr")) i)))
856 (numbered? (org-export-numbered-headline-p headline info))
857 (notoc? (org-export-excluded-from-toc-p headline info))
858 (command
859 (and
860 (not (org-export-low-level-p headline info))
861 (let ((class (plist-get info :texinfo-class)))
862 (pcase (assoc class (plist-get info :texinfo-classes))
863 (`(,_ ,_ . ,sections)
864 (pcase (nth (1- (org-export-get-relative-level headline info))
865 sections)
866 (`(,numbered ,unnumbered ,unnumbered-no-toc ,appendix)
867 (cond
868 ((org-not-nil
869 (org-export-get-node-property :APPENDIX headline t))
870 appendix)
871 (numbered? numbered)
872 (index unnumbered)
873 (notoc? unnumbered-no-toc)
874 (t unnumbered)))
875 (`nil nil)
876 (_ (user-error "Invalid Texinfo class specification: %S"
877 class))))
878 (_ (user-error "Unknown Texinfo class: %S" class))))))
879 (todo
880 (and (plist-get info :with-todo-keywords)
881 (let ((todo (org-element-property :todo-keyword headline)))
882 (and todo (org-export-data todo info)))))
883 (todo-type (and todo (org-element-property :todo-type headline)))
884 (tags (and (plist-get info :with-tags)
885 (org-export-get-tags headline info)))
886 (priority (and (plist-get info :with-priority)
887 (org-element-property :priority headline)))
888 (text (org-texinfo--sanitize-title
889 (org-element-property :title headline) info))
890 (full-text
891 (funcall (plist-get info :texinfo-format-headline-function)
892 todo todo-type priority text tags))
893 (contents
894 (concat "\n"
895 (if (org-string-nw-p contents) (concat "\n" contents) "")
896 (and index (format "\n@printindex %s\n" index)))))
897 (if (not command)
898 (concat (and (org-export-first-sibling-p headline info)
899 (format "@%s\n" (if numbered? 'enumerate 'itemize)))
900 "@item\n" full-text "\n"
901 contents
902 (if (org-export-last-sibling-p headline info)
903 (format "@end %s" (if numbered? 'enumerate 'itemize))
904 "\n"))
905 (concat
906 ;; Even if HEADLINE is using @subheading and al., leave an
907 ;; anchor so cross-references in the Org document still work.
908 (format (if notoc? "@anchor{%s}\n" "@node %s\n")
909 (org-texinfo--get-node headline info))
910 (format command full-text)
911 contents))))))
913 (defun org-texinfo-format-headline-default-function
914 (todo _todo-type priority text tags)
915 "Default format function for a headline.
916 See `org-texinfo-format-headline-function' for details."
917 (concat (when todo (format "@strong{%s} " todo))
918 (when priority (format "@emph{#%s} " priority))
919 text
920 (when tags (format " :%s:" (mapconcat 'identity tags ":")))))
922 ;;;; Inline Src Block
924 (defun org-texinfo-inline-src-block (inline-src-block _contents _info)
925 "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
926 CONTENTS holds the contents of the item. INFO is a plist holding
927 contextual information."
928 (format "@code{%s}"
929 (org-texinfo--sanitize-content
930 (org-element-property :value inline-src-block))))
932 ;;;; Inlinetask
934 (defun org-texinfo-inlinetask (inlinetask contents info)
935 "Transcode an INLINETASK element from Org to Texinfo.
936 CONTENTS holds the contents of the block. INFO is a plist
937 holding contextual information."
938 (let ((title (org-export-data (org-element-property :title inlinetask) info))
939 (todo (and (plist-get info :with-todo-keywords)
940 (let ((todo (org-element-property :todo-keyword inlinetask)))
941 (and todo (org-export-data todo info)))))
942 (todo-type (org-element-property :todo-type inlinetask))
943 (tags (and (plist-get info :with-tags)
944 (org-export-get-tags inlinetask info)))
945 (priority (and (plist-get info :with-priority)
946 (org-element-property :priority inlinetask))))
947 (funcall (plist-get info :texinfo-format-inlinetask-function)
948 todo todo-type priority title tags contents)))
950 (defun org-texinfo-format-inlinetask-default-function
951 (todo _todo-type priority title tags contents)
952 "Default format function for a inlinetasks.
953 See `org-texinfo-format-inlinetask-function' for details."
954 (let ((full-title
955 (concat (when todo (format "@strong{%s} " todo))
956 (when priority (format "#%c " priority))
957 title
958 (when tags (format ":%s:" (mapconcat #'identity tags ":"))))))
959 (format "@center %s\n\n%s\n" full-title contents)))
961 ;;;; Italic
963 (defun org-texinfo-italic (_italic contents info)
964 "Transcode ITALIC from Org to Texinfo.
965 CONTENTS is the text with italic markup. INFO is a plist holding
966 contextual information."
967 (org-texinfo--text-markup contents 'italic info))
969 ;;;; Item
971 (defun org-texinfo-item (item contents info)
972 "Transcode an ITEM element from Org to Texinfo.
973 CONTENTS holds the contents of the item. INFO is a plist holding
974 contextual information."
975 (let* ((tag (org-element-property :tag item))
976 (split (org-string-nw-p
977 (org-export-read-attribute :attr_texinfo
978 (org-element-property :parent item)
979 :sep)))
980 (items (and tag
981 (let ((tag (org-export-data tag info)))
982 (if split
983 (split-string tag (regexp-quote split) t "[ \t\n]+")
984 (list tag))))))
985 (format "%s\n%s"
986 (pcase items
987 (`nil "@item")
988 (`(,item) (concat "@item " item))
989 (`(,item . ,items)
990 (concat "@item " item "\n"
991 (mapconcat (lambda (i) (concat "@itemx " i))
992 items
993 "\n"))))
994 (or contents ""))))
996 ;;;; Keyword
998 (defun org-texinfo-keyword (keyword _contents info)
999 "Transcode a KEYWORD element from Org to Texinfo.
1000 CONTENTS is nil. INFO is a plist holding contextual information."
1001 (let ((value (org-element-property :value keyword)))
1002 (pcase (org-element-property :key keyword)
1003 ("TEXINFO" value)
1004 ("CINDEX" (format "@cindex %s" value))
1005 ("FINDEX" (format "@findex %s" value))
1006 ("KINDEX" (format "@kindex %s" value))
1007 ("PINDEX" (format "@pindex %s" value))
1008 ("TINDEX" (format "@tindex %s" value))
1009 ("VINDEX" (format "@vindex %s" value))
1010 ("TOC"
1011 (cond ((string-match-p "\\<tables\\>" value)
1012 (concat "@listoffloats "
1013 (org-export-translate "Table" :utf-8 info)))
1014 ((string-match-p "\\<listings\\>" value)
1015 (concat "@listoffloats "
1016 (org-export-translate "Listing" :utf-8 info))))))))
1018 ;;;; Line Break
1020 (defun org-texinfo-line-break (_line-break _contents _info)
1021 "Transcode a LINE-BREAK object from Org to Texinfo.
1022 CONTENTS is nil. INFO is a plist holding contextual information."
1023 "@*\n")
1025 ;;;; Link
1027 (defun org-texinfo--@ref (datum description info)
1028 "Return @ref command for element or object DATUM.
1029 DESCRIPTION is the printed name of the section, as a string, or
1030 nil."
1031 (let ((node-name (org-texinfo--get-node datum info))
1032 ;; Sanitize DESCRIPTION for cross-reference use. In
1033 ;; particular, remove colons as they seem to cause pain (even
1034 ;; within @asis{...}) to the Texinfo reader.
1035 (title (and description
1036 (replace-regexp-in-string
1037 "[ \t]*:+" ""
1038 (replace-regexp-in-string "," "@comma{}" description)))))
1039 (if (or (not title) (equal title node-name))
1040 (format "@ref{%s}" node-name)
1041 (format "@ref{%s, , %s}" node-name title))))
1043 (defun org-texinfo-link (link desc info)
1044 "Transcode a LINK object from Org to Texinfo.
1045 DESC is the description part of the link, or the empty string.
1046 INFO is a plist holding contextual information. See
1047 `org-export-data'."
1048 (let* ((type (org-element-property :type link))
1049 (raw-path (org-element-property :path link))
1050 ;; Ensure DESC really exists, or set it to nil.
1051 (desc (and (not (string= desc "")) desc))
1052 (path (cond
1053 ((member type '("http" "https" "ftp"))
1054 (concat type ":" raw-path))
1055 ((string= type "file") (org-export-file-uri raw-path))
1056 (t raw-path))))
1057 (cond
1058 ((org-export-custom-protocol-maybe link desc 'texinfo))
1059 ((org-export-inline-image-p link org-texinfo-inline-image-rules)
1060 (org-texinfo--inline-image link info))
1061 ((equal type "radio")
1062 (let ((destination (org-export-resolve-radio-link link info)))
1063 (if (not destination) desc
1064 (org-texinfo--@ref destination desc info))))
1065 ((member type '("custom-id" "id" "fuzzy"))
1066 (let ((destination
1067 (if (equal type "fuzzy")
1068 (org-export-resolve-fuzzy-link link info)
1069 (org-export-resolve-id-link link info))))
1070 (pcase (org-element-type destination)
1071 (`nil
1072 (format org-texinfo-link-with-unknown-path-format
1073 (org-texinfo--sanitize-content path)))
1074 ;; Id link points to an external file.
1075 (`plain-text
1076 (if desc (format "@uref{file://%s,%s}" destination desc)
1077 (format "@uref{file://%s}" destination)))
1078 ((or `headline
1079 ;; Targets within headlines cannot be turned into
1080 ;; @anchor{}, so we refer to the headline parent
1081 ;; directly.
1082 (and `target
1083 (guard (eq 'headline
1084 (org-element-type
1085 (org-element-property :parent destination))))))
1086 (let ((headline (org-element-lineage destination '(headline) t)))
1087 (org-texinfo--@ref headline desc info)))
1088 (_ (org-texinfo--@ref destination desc info)))))
1089 ((string= type "mailto")
1090 (format "@email{%s}"
1091 (concat (org-texinfo--sanitize-content path)
1092 (and desc (concat ", " desc)))))
1093 ;; External link with a description part.
1094 ((and path desc) (format "@uref{%s, %s}" path desc))
1095 ;; External link without a description part.
1096 (path (format "@uref{%s}" path))
1097 ;; No path, only description. Try to do something useful.
1099 (format (plist-get info :texinfo-link-with-unknown-path-format) desc)))))
1101 (defun org-texinfo--inline-image (link info)
1102 "Return Texinfo code for an inline image.
1103 LINK is the link pointing to the inline image. INFO is the
1104 current state of the export, as a plist."
1105 (let* ((parent (org-export-get-parent-element link))
1106 (label (and (org-element-property :name parent)
1107 (org-texinfo--get-node parent info)))
1108 (caption (org-export-get-caption parent))
1109 (shortcaption (org-export-get-caption parent t))
1110 (path (org-element-property :path link))
1111 (filename
1112 (file-name-sans-extension
1113 (if (file-name-absolute-p path) (expand-file-name path) path)))
1114 (extension (file-name-extension path))
1115 (attributes (org-export-read-attribute :attr_texinfo parent))
1116 (height (or (plist-get attributes :height) ""))
1117 (width (or (plist-get attributes :width) ""))
1118 (alt (or (plist-get attributes :alt) ""))
1119 (image (format "@image{%s,%s,%s,%s,%s}"
1120 filename width height alt extension)))
1121 (cond ((or caption shortcaption)
1122 (org-texinfo--wrap-float image
1123 info
1124 (org-export-translate "Figure" :utf-8 info)
1125 label
1126 caption
1127 shortcaption))
1128 (label (concat "@anchor{" label "}\n" image))
1129 (t image))))
1132 ;;;; Menu
1134 (defun org-texinfo-make-menu (scope info &optional master)
1135 "Create the menu for inclusion in the Texinfo document.
1137 SCOPE is a headline or a full parse tree. INFO is the
1138 communication channel, as a plist.
1140 When optional argument MASTER is non-nil, generate a master menu,
1141 including detailed node listing."
1142 (let ((menu (org-texinfo--build-menu scope info)))
1143 (when (org-string-nw-p menu)
1144 (org-element-normalize-string
1145 (format
1146 "@menu\n%s@end menu"
1147 (concat menu
1148 (when master
1149 (let ((detailmenu
1150 (org-texinfo--build-menu
1151 scope info
1152 (let ((toc-depth (plist-get info :with-toc)))
1153 (if (wholenump toc-depth) toc-depth
1154 org-texinfo-max-toc-depth)))))
1155 (when (org-string-nw-p detailmenu)
1156 (concat "\n@detailmenu\n"
1157 "--- The Detailed Node Listing ---\n\n"
1158 detailmenu
1159 "@end detailmenu\n"))))))))))
1161 (defun org-texinfo--build-menu (scope info &optional level)
1162 "Build menu for entries within SCOPE.
1163 SCOPE is a headline or a full parse tree. INFO is a plist
1164 containing contextual information. When optional argument LEVEL
1165 is an integer, build the menu recursively, down to this depth."
1166 (cond
1167 ((not level)
1168 (org-texinfo--format-entries (org-texinfo--menu-entries scope info) info))
1169 ((zerop level) "\n")
1171 (mapconcat
1172 (lambda (h)
1173 (let ((entries (org-texinfo--menu-entries h info)))
1174 (when entries
1175 (concat
1176 (format "%s\n\n%s\n"
1177 (org-export-data (org-export-get-alt-title h info) info)
1178 (org-texinfo--format-entries entries info))
1179 (org-texinfo--build-menu h info (1- level))))))
1180 (org-texinfo--menu-entries scope info)
1181 ""))))
1183 (defun org-texinfo--format-entries (entries info)
1184 "Format all direct menu entries in SCOPE, as a string.
1185 SCOPE is either a headline or a full Org document. INFO is
1186 a plist containing contextual information."
1187 (org-element-normalize-string
1188 (mapconcat
1189 (lambda (h)
1190 (let* ((title
1191 ;; Colons are used as a separator between title and node
1192 ;; name. Remove them.
1193 (replace-regexp-in-string
1194 "[ \t]+:+" ""
1195 (org-texinfo--sanitize-title
1196 (org-export-get-alt-title h info) info)))
1197 (node (org-texinfo--get-node h info))
1198 (entry (concat "* " title ":"
1199 (if (string= title node) ":"
1200 (concat " " node ". "))))
1201 (desc (org-element-property :DESCRIPTION h)))
1202 (if (not desc) entry
1203 (format (format "%%-%ds %%s" org-texinfo-node-description-column)
1204 entry desc))))
1205 entries "\n")))
1207 (defun org-texinfo--menu-entries (scope info)
1208 "List direct children in SCOPE needing a menu entry.
1209 SCOPE is a headline or a full parse tree. INFO is a plist
1210 holding contextual information."
1211 (let* ((cache (or (plist-get info :texinfo-entries-cache)
1212 (plist-get (plist-put info :texinfo-entries-cache
1213 (make-hash-table :test #'eq))
1214 :texinfo-entries-cache)))
1215 (cached-entries (gethash scope cache 'no-cache)))
1216 (if (not (eq cached-entries 'no-cache)) cached-entries
1217 (puthash scope
1218 (cl-remove-if
1219 (lambda (h)
1220 (org-not-nil (org-export-get-node-property :COPYING h t)))
1221 (org-export-collect-headlines info 1 scope))
1222 cache))))
1224 ;;;; Node Property
1226 (defun org-texinfo-node-property (node-property _contents _info)
1227 "Transcode a NODE-PROPERTY element from Org to Texinfo.
1228 CONTENTS is nil. INFO is a plist holding contextual
1229 information."
1230 (format "%s:%s"
1231 (org-element-property :key node-property)
1232 (let ((value (org-element-property :value node-property)))
1233 (if value (concat " " value) ""))))
1235 ;;;; Paragraph
1237 (defun org-texinfo-paragraph (_paragraph contents _info)
1238 "Transcode a PARAGRAPH element from Org to Texinfo.
1239 CONTENTS is the contents of the paragraph, as a string. INFO is
1240 the plist used as a communication channel."
1241 contents)
1243 ;;;; Plain List
1245 (defun org-texinfo-plain-list (plain-list contents info)
1246 "Transcode a PLAIN-LIST element from Org to Texinfo.
1247 CONTENTS is the contents of the list. INFO is a plist holding
1248 contextual information."
1249 (let* ((attr (org-export-read-attribute :attr_texinfo plain-list))
1250 (indic (let ((i (or (plist-get attr :indic)
1251 (plist-get info :texinfo-table-default-markup))))
1252 ;; Allow indicating commands with missing @ sign.
1253 (if (string-prefix-p "@" i) i (concat "@" i))))
1254 (table-type (plist-get attr :table-type))
1255 (type (org-element-property :type plain-list))
1256 (list-type (cond
1257 ((eq type 'ordered) "enumerate")
1258 ((eq type 'unordered) "itemize")
1259 ((member table-type '("ftable" "vtable")) table-type)
1260 (t "table"))))
1261 (format "@%s\n%s@end %s"
1262 (if (eq type 'descriptive) (concat list-type " " indic) list-type)
1263 contents
1264 list-type)))
1266 ;;;; Plain Text
1268 (defun org-texinfo-plain-text (text info)
1269 "Transcode a TEXT string from Org to Texinfo.
1270 TEXT is the string to transcode. INFO is a plist holding
1271 contextual information."
1272 ;; First protect @, { and }.
1273 (let ((output (org-texinfo--sanitize-content text)))
1274 ;; Activate smart quotes. Be sure to provide original TEXT string
1275 ;; since OUTPUT may have been modified.
1276 (when (plist-get info :with-smart-quotes)
1277 (setq output
1278 (org-export-activate-smart-quotes output :texinfo info text)))
1279 ;; LaTeX into @LaTeX{} and TeX into @TeX{}
1280 (let ((case-fold-search nil))
1281 (setq output (replace-regexp-in-string "\\(?:La\\)?TeX" "@\\&{}" output)))
1282 ;; Convert special strings.
1283 (when (plist-get info :with-special-strings)
1284 (setq output
1285 (replace-regexp-in-string
1286 "\\.\\.\\." "@dots{}"
1287 (replace-regexp-in-string "\\\\-" "@-" output))))
1288 ;; Handle break preservation if required.
1289 (when (plist-get info :preserve-breaks)
1290 (setq output (replace-regexp-in-string
1291 "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" output)))
1292 ;; Return value.
1293 output))
1295 ;;;; Planning
1297 (defun org-texinfo-planning (planning _contents info)
1298 "Transcode a PLANNING element from Org to Texinfo.
1299 CONTENTS is nil. INFO is a plist holding contextual
1300 information."
1301 (concat
1302 "@noindent"
1303 (mapconcat
1304 'identity
1305 (delq nil
1306 (list
1307 (let ((closed (org-element-property :closed planning)))
1308 (when closed
1309 (concat
1310 (format "@strong{%s} " org-closed-string)
1311 (format (plist-get info :texinfo-inactive-timestamp-format)
1312 (org-timestamp-translate closed)))))
1313 (let ((deadline (org-element-property :deadline planning)))
1314 (when deadline
1315 (concat
1316 (format "@strong{%s} " org-deadline-string)
1317 (format (plist-get info :texinfo-active-timestamp-format)
1318 (org-timestamp-translate deadline)))))
1319 (let ((scheduled (org-element-property :scheduled planning)))
1320 (when scheduled
1321 (concat
1322 (format "@strong{%s} " org-scheduled-string)
1323 (format (plist-get info :texinfo-active-timestamp-format)
1324 (org-timestamp-translate scheduled)))))))
1325 " ")
1326 "@*"))
1328 ;;;; Property Drawer
1330 (defun org-texinfo-property-drawer (_property-drawer contents _info)
1331 "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
1332 CONTENTS holds the contents of the drawer. INFO is a plist
1333 holding contextual information."
1334 (and (org-string-nw-p contents)
1335 (format "@verbatim\n%s@end verbatim" contents)))
1337 ;;;; Quote Block
1339 (defun org-texinfo-quote-block (quote-block contents _info)
1340 "Transcode a QUOTE-BLOCK element from Org to Texinfo.
1341 CONTENTS holds the contents of the block. INFO is a plist
1342 holding contextual information."
1343 (let* ((title (org-element-property :name quote-block))
1344 (start-quote (concat "@quotation"
1345 (if title
1346 (format " %s" title)))))
1347 (format "%s\n%s@end quotation" start-quote contents)))
1349 ;;;; Radio Target
1351 (defun org-texinfo-radio-target (radio-target text info)
1352 "Transcode a RADIO-TARGET object from Org to Texinfo.
1353 TEXT is the text of the target. INFO is a plist holding
1354 contextual information."
1355 (format "@anchor{%s}%s"
1356 (org-texinfo--get-node radio-target info)
1357 text))
1359 ;;;; Section
1361 (defun org-texinfo-section (section contents info)
1362 "Transcode a SECTION element from Org to Texinfo.
1363 CONTENTS holds the contents of the section. INFO is a plist
1364 holding contextual information."
1365 (let ((parent (org-export-get-parent-headline section)))
1366 (when parent ;first section is handled in `org-texinfo-template'
1367 (org-trim
1368 (concat contents
1369 "\n"
1370 (and (not (org-export-excluded-from-toc-p parent info))
1371 (org-texinfo-make-menu parent info)))))))
1373 ;;;; Special Block
1375 (defun org-texinfo-special-block (special-block contents _info)
1376 "Transcode a SPECIAL-BLOCK element from Org to Texinfo.
1377 CONTENTS holds the contents of the block. INFO is a plist used
1378 as a communication channel."
1379 (let ((opt (org-export-read-attribute :attr_texinfo special-block :options))
1380 (type (org-element-property :type special-block)))
1381 (format "@%s%s\n%s@end %s"
1382 type
1383 (if opt (concat " " opt) "")
1384 (or contents "")
1385 type)))
1387 ;;;; Src Block
1389 (defun org-texinfo-src-block (src-block _contents info)
1390 "Transcode a SRC-BLOCK element from Org to Texinfo.
1391 CONTENTS holds the contents of the item. INFO is a plist holding
1392 contextual information."
1393 (let* ((lisp (string-match-p "lisp"
1394 (org-element-property :language src-block)))
1395 (code (org-texinfo--sanitize-content
1396 (org-export-format-code-default src-block info)))
1397 (value (format
1398 (if lisp "@lisp\n%s@end lisp" "@example\n%s@end example")
1399 code))
1400 (caption (org-export-get-caption src-block))
1401 (shortcaption (org-export-get-caption src-block t)))
1402 (if (not (or caption shortcaption)) value
1403 (org-texinfo--wrap-float value
1404 info
1405 (org-export-translate "Listing" :utf-8 info)
1406 (org-texinfo--get-node src-block info)
1407 caption
1408 shortcaption))))
1410 ;;;; Statistics Cookie
1412 (defun org-texinfo-statistics-cookie (statistics-cookie _contents _info)
1413 "Transcode a STATISTICS-COOKIE object from Org to Texinfo.
1414 CONTENTS is nil. INFO is a plist holding contextual information."
1415 (org-element-property :value statistics-cookie))
1418 ;;;; Strike-through
1420 (defun org-texinfo-strike-through (_strike-through contents info)
1421 "Transcode STRIKE-THROUGH from Org to Texinfo.
1422 CONTENTS is the text with strike-through markup. INFO is a plist
1423 holding contextual information."
1424 (org-texinfo--text-markup contents 'strike-through info))
1426 ;;;; Subscript
1428 (defun org-texinfo-subscript (_subscript contents _info)
1429 "Transcode a SUBSCRIPT object from Org to Texinfo.
1430 CONTENTS is the contents of the object. INFO is a plist holding
1431 contextual information."
1432 (format "@math{_%s}" contents))
1434 ;;;; Superscript
1436 (defun org-texinfo-superscript (_superscript contents _info)
1437 "Transcode a SUPERSCRIPT object from Org to Texinfo.
1438 CONTENTS is the contents of the object. INFO is a plist holding
1439 contextual information."
1440 (format "@math{^%s}" contents))
1442 ;;;; Table
1444 (defun org-texinfo-table (table contents info)
1445 "Transcode a TABLE element from Org to Texinfo.
1446 CONTENTS is the contents of the table. INFO is a plist holding
1447 contextual information."
1448 (if (eq (org-element-property :type table) 'table.el)
1449 (format "@verbatim\n%s@end verbatim"
1450 (org-element-normalize-string
1451 (org-element-property :value table)))
1452 (let* ((col-width (org-export-read-attribute :attr_texinfo table :columns))
1453 (columns
1454 (if col-width (format "@columnfractions %s" col-width)
1455 (org-texinfo-table-column-widths table info)))
1456 (caption (org-export-get-caption table))
1457 (shortcaption (org-export-get-caption table t))
1458 (table-str (format "@multitable %s\n%s@end multitable"
1459 columns
1460 contents)))
1461 (if (not (or caption shortcaption)) table-str
1462 (org-texinfo--wrap-float table-str
1463 info
1464 (org-export-translate "Table" :utf-8 info)
1465 (org-texinfo--get-node table info)
1466 caption
1467 shortcaption)))))
1469 (defun org-texinfo-table-column-widths (table info)
1470 "Determine the largest table cell in each column to process alignment.
1471 TABLE is the table element to transcode. INFO is a plist used as
1472 a communication channel."
1473 (let ((widths (make-vector (cdr (org-export-table-dimensions table info)) 0)))
1474 (org-element-map table 'table-row
1475 (lambda (row)
1476 (let ((idx 0))
1477 (org-element-map row 'table-cell
1478 (lambda (cell)
1479 ;; Length of the cell in the original buffer is only an
1480 ;; approximation of the length of the cell in the
1481 ;; output. It can sometimes fail (e.g. it considers
1482 ;; "/a/" being larger than "ab").
1483 (let ((w (- (org-element-property :contents-end cell)
1484 (org-element-property :contents-begin cell))))
1485 (aset widths idx (max w (aref widths idx))))
1486 (cl-incf idx))
1487 info)))
1488 info)
1489 (format "{%s}" (mapconcat (lambda (w) (make-string w ?a)) widths "} {"))))
1491 ;;;; Table Cell
1493 (defun org-texinfo-table-cell (table-cell contents info)
1494 "Transcode a TABLE-CELL element from Org to Texinfo.
1495 CONTENTS is the cell contents. INFO is a plist used as
1496 a communication channel."
1497 (concat
1498 (let ((scientific-notation
1499 (plist-get info :texinfo-table-scientific-notation)))
1500 (if (and contents
1501 scientific-notation
1502 (string-match orgtbl-exp-regexp contents))
1503 ;; Use appropriate format string for scientific notation.
1504 (format scientific-notation
1505 (match-string 1 contents)
1506 (match-string 2 contents))
1507 contents))
1508 (when (org-export-get-next-element table-cell info) "\n@tab ")))
1510 ;;;; Table Row
1512 (defun org-texinfo-table-row (table-row contents info)
1513 "Transcode a TABLE-ROW element from Org to Texinfo.
1514 CONTENTS is the contents of the row. INFO is a plist used as
1515 a communication channel."
1516 ;; Rules are ignored since table separators are deduced from
1517 ;; borders of the current row.
1518 (when (eq (org-element-property :type table-row) 'standard)
1519 (let ((rowgroup-tag
1520 (if (and (= 1 (org-export-table-row-group table-row info))
1521 (org-export-table-has-header-p
1522 (org-export-get-parent-table table-row) info))
1523 "@headitem "
1524 "@item ")))
1525 (concat rowgroup-tag contents "\n"))))
1527 ;;;; Target
1529 (defun org-texinfo-target (target _contents info)
1530 "Transcode a TARGET object from Org to Texinfo.
1531 CONTENTS is nil. INFO is a plist holding contextual
1532 information."
1533 (format "@anchor{%s}" (org-texinfo--get-node target info)))
1535 ;;;; Timestamp
1537 (defun org-texinfo-timestamp (timestamp _contents info)
1538 "Transcode a TIMESTAMP object from Org to Texinfo.
1539 CONTENTS is nil. INFO is a plist holding contextual
1540 information."
1541 (let ((value (org-texinfo-plain-text
1542 (org-timestamp-translate timestamp) info)))
1543 (pcase (org-element-property :type timestamp)
1544 ((or `active `active-range)
1545 (format (plist-get info :texinfo-active-timestamp-format) value))
1546 ((or `inactive `inactive-range)
1547 (format (plist-get info :texinfo-inactive-timestamp-format) value))
1548 (_ (format (plist-get info :texinfo-diary-timestamp-format) value)))))
1550 ;;;; Underline
1552 (defun org-texinfo-underline (_underline contents info)
1553 "Transcode UNDERLINE from Org to Texinfo.
1554 CONTENTS is the text with underline markup. INFO is a plist
1555 holding contextual information."
1556 (org-texinfo--text-markup contents 'underline info))
1558 ;;;; Verbatim
1560 (defun org-texinfo-verbatim (verbatim _contents info)
1561 "Transcode a VERBATIM object from Org to Texinfo.
1562 CONTENTS is nil. INFO is a plist used as a communication
1563 channel."
1564 (org-texinfo--text-markup
1565 (org-element-property :value verbatim) 'verbatim info))
1567 ;;;; Verse Block
1569 (defun org-texinfo-verse-block (_verse-block contents _info)
1570 "Transcode a VERSE-BLOCK element from Org to Texinfo.
1571 CONTENTS is verse block contents. INFO is a plist holding
1572 contextual information."
1573 (format "@display\n%s@end display" contents))
1576 ;;; Interactive functions
1578 ;;;###autoload
1579 (defun org-texinfo-export-to-texinfo
1580 (&optional async subtreep visible-only body-only ext-plist)
1581 "Export current buffer to a Texinfo file.
1583 If narrowing is active in the current buffer, only export its
1584 narrowed part.
1586 If a region is active, export that region.
1588 A non-nil optional argument ASYNC means the process should happen
1589 asynchronously. The resulting file should be accessible through
1590 the `org-export-stack' interface.
1592 When optional argument SUBTREEP is non-nil, export the sub-tree
1593 at point, extracting information from the headline properties
1594 first.
1596 When optional argument VISIBLE-ONLY is non-nil, don't export
1597 contents of hidden elements.
1599 When optional argument BODY-ONLY is non-nil, only write code
1600 between \"\\begin{document}\" and \"\\end{document}\".
1602 EXT-PLIST, when provided, is a property list with external
1603 parameters overriding Org default settings, but still inferior to
1604 file-local settings.
1606 Return output file's name."
1607 (interactive)
1608 (let ((outfile (org-export-output-file-name ".texi" subtreep))
1609 (org-export-coding-system org-texinfo-coding-system))
1610 (org-export-to-file 'texinfo outfile
1611 async subtreep visible-only body-only ext-plist)))
1613 ;;;###autoload
1614 (defun org-texinfo-export-to-info
1615 (&optional async subtreep visible-only body-only ext-plist)
1616 "Export current buffer to Texinfo then process through to INFO.
1618 If narrowing is active in the current buffer, only export its
1619 narrowed part.
1621 If a region is active, export that region.
1623 A non-nil optional argument ASYNC means the process should happen
1624 asynchronously. The resulting file should be accessible through
1625 the `org-export-stack' interface.
1627 When optional argument SUBTREEP is non-nil, export the sub-tree
1628 at point, extracting information from the headline properties
1629 first.
1631 When optional argument VISIBLE-ONLY is non-nil, don't export
1632 contents of hidden elements.
1634 When optional argument BODY-ONLY is non-nil, only write code
1635 between \"\\begin{document}\" and \"\\end{document}\".
1637 EXT-PLIST, when provided, is a property list with external
1638 parameters overriding Org default settings, but still inferior to
1639 file-local settings.
1641 When optional argument PUB-DIR is set, use it as the publishing
1642 directory.
1644 Return INFO file's name."
1645 (interactive)
1646 (let ((outfile (org-export-output-file-name ".texi" subtreep))
1647 (org-export-coding-system org-texinfo-coding-system))
1648 (org-export-to-file 'texinfo outfile
1649 async subtreep visible-only body-only ext-plist
1650 (lambda (file) (org-texinfo-compile file)))))
1652 ;;;###autoload
1653 (defun org-texinfo-publish-to-texinfo (plist filename pub-dir)
1654 "Publish an org file to Texinfo.
1656 FILENAME is the filename of the Org file to be published. PLIST
1657 is the property list for the given project. PUB-DIR is the
1658 publishing directory.
1660 Return output file name."
1661 (org-publish-org-to 'texinfo filename ".texi" plist pub-dir))
1663 ;;;###autoload
1664 (defun org-texinfo-convert-region-to-texinfo ()
1665 "Assume the current region has Org syntax, and convert it to Texinfo.
1666 This can be used in any buffer. For example, you can write an
1667 itemized list in Org syntax in an Texinfo buffer and use this
1668 command to convert it."
1669 (interactive)
1670 (org-export-replace-region-by 'texinfo))
1672 (defun org-texinfo-compile (file)
1673 "Compile a texinfo file.
1675 FILE is the name of the file being compiled. Processing is done
1676 through the command specified in `org-texinfo-info-process',
1677 which see. Output is redirected to \"*Org INFO Texinfo Output*\"
1678 buffer.
1680 Return INFO file name or an error if it couldn't be produced."
1681 (message "Processing Texinfo file %s..." file)
1682 (let* ((log-name "*Org INFO Texinfo Output*")
1683 (log (get-buffer-create log-name))
1684 (output
1685 (org-compile-file file org-texinfo-info-process "info"
1686 (format "See %S for details" log-name)
1687 log)))
1688 (when org-texinfo-remove-logfiles
1689 (let ((base (file-name-sans-extension output)))
1690 (dolist (ext org-texinfo-logfiles-extensions)
1691 (let ((file (concat base "." ext)))
1692 (when (file-exists-p file) (delete-file file))))))
1693 (message "Process completed.")
1694 output))
1697 (provide 'ox-texinfo)
1699 ;; Local variables:
1700 ;; generated-autoload-file: "org-loaddefs.el"
1701 ;; End:
1703 ;;; ox-texinfo.el ends here