Fix links without contents in table of contents' entries
[org-mode/org-tableheadings.git] / lisp / ox-texinfo.el
blobe6aa3e5bcf0ee249ea6327f2a84b1022d8d844f5
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 :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-table-default-markup nil nil org-texinfo-table-default-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" "@appendix %s")
150 ("@section %s" "@unnumberedsec %s" "@appendixsec %s")
151 ("@subsection %s" "@unnumberedsubsec %s" "@appendixsubsec %s")
152 ("@subsubsection %s" "@unnumberedsubsubsec %s" "@appendixsubsubsec %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 a class
156 definition:
158 (class-name
159 header-string
160 (numbered-1 unnumbered-1 appendix-1)
161 (numbered-2 unnumbered-2 appendix-2)
162 ...)
165 The header string
166 -----------------
168 The header string is inserted in the header of the generated
169 document, right after \"@setfilename\" and \"@settitle\"
170 commands.
172 If it contains the special string
174 \"@documentencoding AUTO\"
176 \"AUTO\" will be replaced with an appropriate coding system. See
177 `org-texinfo-coding-system' for more information. Likewise, if
178 the string contains the special string
180 \"@documentlanguage AUTO\"
182 \"AUTO\" will be replaced with the language defined in the
183 buffer, through #+LANGUAGE keyword, or globally, with
184 `org-export-default-language', which see.
187 The sectioning structure
188 ------------------------
190 The sectioning structure of the class is given by the elements
191 following the header string. For each sectioning level, a number
192 of strings is specified. A %s formatter is mandatory in each
193 section string and will be replaced by the title of the section."
194 :group 'org-export-texinfo
195 :version "26.1"
196 :package-version '(Org . "9.1")
197 :type '(repeat
198 (list (string :tag "Texinfo class")
199 (string :tag "Texinfo header")
200 (repeat :tag "Levels" :inline t
201 (choice
202 (list :tag "Heading"
203 (string :tag " numbered")
204 (string :tag "unnumbered")
205 (string :tag " appendix")))))))
207 ;;;; Headline
209 (defcustom org-texinfo-format-headline-function
210 'org-texinfo-format-headline-default-function
211 "Function to format headline text.
213 This function will be called with 5 arguments:
214 TODO the todo keyword (string or nil).
215 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
216 PRIORITY the priority of the headline (integer or nil)
217 TEXT the main headline text (string).
218 TAGS the tags as a list of strings (list of strings or nil).
220 The function result will be used in the section format string."
221 :group 'org-export-texinfo
222 :type 'function
223 :version "26.1"
224 :package-version '(Org . "8.3"))
226 ;;;; Node listing (menu)
228 (defcustom org-texinfo-node-description-column 32
229 "Column at which to start the description in the node listings.
230 If a node title is greater than this length, the description will
231 be placed after the end of the title."
232 :group 'org-export-texinfo
233 :type 'integer)
235 ;;;; Timestamps
237 (defcustom org-texinfo-active-timestamp-format "@emph{%s}"
238 "A printf format string to be applied to active timestamps."
239 :group 'org-export-texinfo
240 :type 'string)
242 (defcustom org-texinfo-inactive-timestamp-format "@emph{%s}"
243 "A printf format string to be applied to inactive timestamps."
244 :group 'org-export-texinfo
245 :type 'string)
247 (defcustom org-texinfo-diary-timestamp-format "@emph{%s}"
248 "A printf format string to be applied to diary timestamps."
249 :group 'org-export-texinfo
250 :type 'string)
252 ;;;; Links
254 (defcustom org-texinfo-link-with-unknown-path-format "@indicateurl{%s}"
255 "Format string for links with unknown path type."
256 :group 'org-export-texinfo
257 :type 'string)
259 ;;;; Tables
261 (defcustom org-texinfo-tables-verbatim nil
262 "When non-nil, tables are exported verbatim."
263 :group 'org-export-texinfo
264 :type 'boolean)
266 (defcustom org-texinfo-table-scientific-notation "%s\\,(%s)"
267 "Format string to display numbers in scientific notation.
269 The format should have \"%s\" twice, for mantissa and exponent
270 \(i.e. \"%s\\\\times10^{%s}\").
272 When nil, no transformation is made."
273 :group 'org-export-texinfo
274 :type '(choice
275 (string :tag "Format string")
276 (const :tag "No formatting" nil)))
278 (defcustom org-texinfo-table-default-markup "@asis"
279 "Default markup for first column in two-column tables.
281 This should an indicating command, e.g., \"@code\", \"@kbd\" or
282 \"@samp\".
284 It can be overridden locally using the \":indic\" attribute."
285 :group 'org-export-texinfo
286 :type 'string
287 :version "26.1"
288 :package-version '(Org . "9.1")
289 :safe #'stringp)
291 ;;;; Text markup
293 (defcustom org-texinfo-text-markup-alist '((bold . "@strong{%s}")
294 (code . code)
295 (italic . "@emph{%s}")
296 (verbatim . samp))
297 "Alist of Texinfo expressions to convert text markup.
299 The key must be a symbol among `bold', `code', `italic',
300 `strike-through', `underscore' and `verbatim'. The value is
301 a formatting string to wrap fontified text with.
303 Value can also be set to the following symbols: `verb', `samp'
304 and `code'. With the first one, Org uses \"@verb\" to create
305 a format string and selects a delimiter character that isn't in
306 the string. For the other two, Org uses \"@samp\" or \"@code\"
307 to typeset and protects special characters.
309 When no association is found for a given markup, text is returned
310 as-is."
311 :group 'org-export-texinfo
312 :version "26.1"
313 :package-version '(Org . "9.1")
314 :type 'alist
315 :options '(bold code italic strike-through underscore verbatim))
317 ;;;; Drawers
319 (defcustom org-texinfo-format-drawer-function (lambda (_name contents) contents)
320 "Function called to format a drawer in Texinfo code.
322 The function must accept two parameters:
323 NAME the drawer name, like \"LOGBOOK\"
324 CONTENTS the contents of the drawer.
326 The function should return the string to be exported.
328 The default function simply returns the value of CONTENTS."
329 :group 'org-export-texinfo
330 :version "24.4"
331 :package-version '(Org . "8.2")
332 :type 'function)
334 ;;;; Inlinetasks
336 (defcustom org-texinfo-format-inlinetask-function
337 'org-texinfo-format-inlinetask-default-function
338 "Function called to format an inlinetask in Texinfo code.
340 The function must accept six parameters:
341 TODO the todo keyword, as a string
342 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
343 PRIORITY the inlinetask priority, as a string
344 NAME the inlinetask name, as a string.
345 TAGS the inlinetask tags, as a list of strings.
346 CONTENTS the contents of the inlinetask, as a string.
348 The function should return the string to be exported."
349 :group 'org-export-texinfo
350 :type 'function)
352 ;;;; Compilation
354 (defcustom org-texinfo-info-process '("makeinfo --no-split %f")
355 "Commands to process a Texinfo file to an INFO file.
357 This is a list of strings, each of them will be given to the
358 shell as a command. %f in the command will be replaced by the
359 relative file name, %F by the absolute file name, %b by the file
360 base name (i.e. without directory and extension parts), %o by the
361 base directory of the file and %O by the absolute file name of
362 the output file."
363 :group 'org-export-texinfo
364 :version "26.1"
365 :package-version '(Org . "9.1")
366 :type '(repeat :tag "Shell command sequence"
367 (string :tag "Shell command")))
369 (defcustom org-texinfo-logfiles-extensions
370 '("aux" "toc" "cp" "fn" "ky" "pg" "tp" "vr")
371 "The list of file extensions to consider as Texinfo logfiles.
372 The logfiles will be remove if `org-texinfo-remove-logfiles' is
373 non-nil."
374 :group 'org-export-texinfo
375 :type '(repeat (string :tag "Extension")))
377 (defcustom org-texinfo-remove-logfiles t
378 "Non-nil means remove the logfiles produced by compiling a Texinfo file.
379 By default, logfiles are files with these extensions: .aux, .toc,
380 .cp, .fn, .ky, .pg and .tp. To define the set of logfiles to remove,
381 set `org-texinfo-logfiles-extensions'."
382 :group 'org-export-latex
383 :type 'boolean)
385 ;;; Constants
387 (defconst org-texinfo-max-toc-depth 4
388 "Maximum depth for creation of detailed menu listings.
389 Beyond this depth, Texinfo will not recognize the nodes and will
390 cause errors. Left as a constant in case this value ever
391 changes.")
393 (defconst org-texinfo-supported-coding-systems
394 '("US-ASCII" "UTF-8" "ISO-8859-15" "ISO-8859-1" "ISO-8859-2" "koi8-r" "koi8-u")
395 "List of coding systems supported by Texinfo, as strings.
396 Specified coding system will be matched against these strings.
397 If two strings share the same prefix (e.g. \"ISO-8859-1\" and
398 \"ISO-8859-15\"), the most specific one has to be listed first.")
400 (defconst org-texinfo-inline-image-rules
401 (list (cons "file"
402 (regexp-opt '("eps" "pdf" "png" "jpg" "jpeg" "gif" "svg"))))
403 "Rules characterizing image files that can be inlined.")
406 ;;; Internal Functions
408 (defun org-texinfo--filter-section-blank-lines (headline _backend _info)
409 "Filter controlling number of blank lines after a section."
410 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" "\n\n" headline))
412 (defun org-texinfo--normalize-headlines (tree _backend info)
413 "Normalize headlines in TREE.
415 BACK-END is the symbol specifying back-end used for export. INFO
416 is a plist used as a communication channel.
418 Make sure every headline in TREE contains a section, since those
419 are required to install a menu. Also put exactly one blank line
420 at the end of each section.
422 Return new tree."
423 (org-element-map tree 'headline
424 (lambda (hl)
425 (org-element-put-property hl :post-blank 1)
426 (let ((contents (org-element-contents hl)))
427 (when contents
428 (let ((first (org-element-map contents '(headline section)
429 #'identity info t)))
430 (unless (eq (org-element-type first) 'section)
431 (apply #'org-element-set-contents
433 (cons `(section (:parent ,hl)) contents)))))))
434 info)
435 tree)
437 (defun org-texinfo--find-verb-separator (s)
438 "Return a character not used in string S.
439 This is used to choose a separator for constructs like \\verb."
440 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
441 (cl-loop for c across ll
442 when (not (string-match (regexp-quote (char-to-string c)) s))
443 return (char-to-string c))))
445 (defun org-texinfo--text-markup (text markup _info)
446 "Format TEXT depending on MARKUP text markup.
447 INFO is a plist used as a communication channel. See
448 `org-texinfo-text-markup-alist' for details."
449 (pcase (cdr (assq markup org-texinfo-text-markup-alist))
450 (`nil text) ;no markup: return raw text
451 (`code (format "@code{%s}" (org-texinfo--sanitize-content text)))
452 (`samp (format "@samp{%s}" (org-texinfo--sanitize-content text)))
453 (`verb
454 (let ((separator (org-texinfo--find-verb-separator text)))
455 (format "@verb{%s%s%s}" separator text separator)))
456 ;; Else use format string.
457 (fmt (format fmt text))))
459 (defun org-texinfo--get-node (datum info)
460 "Return node or anchor associated to DATUM.
461 DATUM is an element or object. INFO is a plist used as
462 a communication channel. The function guarantees the node or
463 anchor name is unique."
464 (let ((cache (plist-get info :texinfo-node-cache)))
465 (or (cdr (assq datum cache))
466 (let* ((salt 0)
467 (basename
468 (org-texinfo--sanitize-node
469 (if (eq (org-element-type datum) 'headline)
470 (org-texinfo--sanitize-title
471 (org-export-get-alt-title datum info) info)
472 (org-export-get-reference datum info))))
473 (name basename))
474 ;; Ensure NAME is unique and not reserved node name "Top".
475 (while (or (equal name "Top") (rassoc name cache))
476 (setq name (concat basename (format " %d" (cl-incf salt)))))
477 (plist-put info :texinfo-node-cache (cons (cons datum name) cache))
478 name))))
480 (defun org-texinfo--sanitize-node (title)
481 "Bend string TITLE to node line requirements.
482 Trim string and collapse multiple whitespace characters as they
483 are not significant. Replace leading left parenthesis, when
484 followed by a right parenthesis, with a square bracket. Remove
485 periods, commas and colons."
486 (org-trim
487 (replace-regexp-in-string
488 "[ \t]+" " "
489 (replace-regexp-in-string
490 "[:,.]" ""
491 (replace-regexp-in-string "\\`(\\(.*?)\\)" "[\\1" title)))))
493 (defun org-texinfo--sanitize-title (title info)
494 "Make TITLE suitable as a section name.
495 TITLE is a string or a secondary string. INFO is the current
496 export state, as a plist."
497 (org-export-data-with-backend
498 title
499 (org-export-create-backend
500 :parent 'texinfo
501 :transcoders '((footnote-reference . ignore)
502 (link . (lambda (l c i)
503 (or c
504 (org-export-data
505 (org-element-property :raw-link l)
506 i))))
507 (radio-target . (lambda (_r c _i) c))
508 (target . ignore)))
509 info))
511 (defun org-texinfo--sanitize-content (text)
512 "Escape special characters in string TEXT.
513 Special characters are: @ { }"
514 (replace-regexp-in-string "[@{}]" "@\\&" text))
516 (defun org-texinfo--wrap-float (value info &optional type label caption short)
517 "Wrap string VALUE within a @float command.
518 INFO is the current export state, as a plist. TYPE is float
519 type, as a string. LABEL is the cross reference label for the
520 float, as a string. CAPTION and SHORT are, respectively, the
521 caption and shortcaption used for the float, as secondary
522 strings (e.g., returned by `org-export-get-caption')."
523 (let* ((backend
524 (org-export-create-backend
525 :parent 'texinfo
526 :transcoders '((link . (lambda (l c i)
527 (or c
528 (org-export-data
529 (org-element-property :raw-link l)
530 i))))
531 (radio-target . (lambda (_r c _i) c))
532 (target . ignore))))
533 (short-backend
534 (org-export-create-backend
535 :parent 'texinfo
536 :transcoders
537 '((footnote-reference . ignore)
538 (inline-src-block . ignore)
539 (link . (lambda (l c i)
540 (or c
541 (org-export-data
542 (org-element-property :raw-link l)
543 i))))
544 (radio-target . (lambda (_r c _i) c))
545 (target . ignore)
546 (verbatim . ignore))))
547 (short-str
548 (if (and short caption)
549 (format "@shortcaption{%s}\n"
550 (org-export-data-with-backend short short-backend info))
551 ""))
552 (caption-str
553 (if (or short caption)
554 (format "@caption{%s}\n"
555 (org-export-data-with-backend
556 (or caption short)
557 (if (equal short-str "") short-backend backend)
558 info))
559 "")))
560 (format "@float %s%s\n%s\n%s%s@end float"
561 type (if label (concat "," label) "") value caption-str short-str)))
563 ;;; Template
565 (defun org-texinfo-template (contents info)
566 "Return complete document string after Texinfo conversion.
567 CONTENTS is the transcoded contents string. INFO is a plist
568 holding export options."
569 (let ((title (org-export-data (plist-get info :title) info))
570 ;; Copying data is the contents of the first headline in
571 ;; parse tree with a non-nil copying property.
572 (copying (org-element-map (plist-get info :parse-tree) 'headline
573 (lambda (hl)
574 (and (org-not-nil (org-element-property :COPYING hl))
575 (org-element-contents hl)))
576 info t)))
577 (concat
578 "\\input texinfo @c -*- texinfo -*-\n"
579 "@c %**start of header\n"
580 (let ((file (or (plist-get info :texinfo-filename)
581 (let ((f (plist-get info :output-file)))
582 (and f (concat (file-name-sans-extension f) ".info"))))))
583 (and file (format "@setfilename %s\n" file)))
584 (format "@settitle %s\n" title)
585 ;; Insert class-defined header.
586 (org-element-normalize-string
587 (let ((header (nth 1 (assoc (plist-get info :texinfo-class)
588 org-texinfo-classes)))
589 (coding
590 (catch 'coding-system
591 (let ((case-fold-search t)
592 (name (symbol-name (or org-texinfo-coding-system
593 buffer-file-coding-system))))
594 (dolist (system org-texinfo-supported-coding-systems "UTF-8")
595 (when (string-match-p (regexp-quote system) name)
596 (throw 'coding-system system))))))
597 (language (plist-get info :language))
598 (case-fold-search nil))
599 ;; Auto coding system.
600 (replace-regexp-in-string
601 "^@documentencoding \\(AUTO\\)$"
602 coding
603 (replace-regexp-in-string
604 "^@documentlanguage \\(AUTO\\)$" language header t nil 1) t nil 1)))
605 ;; Additional header options set by #+TEXINFO_HEADER.
606 (let ((texinfo-header (plist-get info :texinfo-header)))
607 (and texinfo-header (org-element-normalize-string texinfo-header)))
608 "@c %**end of header\n\n"
609 ;; Additional options set by #+TEXINFO_POST_HEADER.
610 (let ((texinfo-post-header (plist-get info :texinfo-post-header)))
611 (and texinfo-post-header
612 (org-element-normalize-string texinfo-post-header)))
613 ;; Copying.
614 (and copying
615 (format "@copying\n%s@end copying\n\n"
616 (org-element-normalize-string
617 (org-export-data copying info))))
618 ;; Info directory information. Only supply if both title and
619 ;; category are provided.
620 (let ((dircat (plist-get info :texinfo-dircat))
621 (dirtitle
622 (let ((title (plist-get info :texinfo-dirtitle)))
623 (and title
624 (string-match "^\\(?:\\* \\)?\\(.*?\\)\\(\\.\\)?$" title)
625 (format "* %s." (match-string 1 title))))))
626 (when (and dircat dirtitle)
627 (concat "@dircategory " dircat "\n"
628 "@direntry\n"
629 (let ((dirdesc
630 (let ((desc (plist-get info :texinfo-dirdesc)))
631 (cond ((not desc) nil)
632 ((string-suffix-p "." desc) desc)
633 (t (concat desc "."))))))
634 (if dirdesc (format "%-23s %s" dirtitle dirdesc) dirtitle))
635 "\n"
636 "@end direntry\n\n")))
637 ;; Title
638 "@finalout\n"
639 "@titlepage\n"
640 (when (plist-get info :with-title)
641 (concat
642 (format "@title %s\n"
643 (or (plist-get info :texinfo-printed-title) title ""))
644 (let ((subtitle (plist-get info :subtitle)))
645 (when subtitle
646 (format "@subtitle %s\n"
647 (org-export-data subtitle info))))))
648 (when (plist-get info :with-author)
649 (concat
650 ;; Primary author.
651 (let ((author (org-string-nw-p
652 (org-export-data (plist-get info :author) info)))
653 (email (and (plist-get info :with-email)
654 (org-string-nw-p
655 (org-export-data (plist-get info :email) info)))))
656 (cond ((and author email)
657 (format "@author %s (@email{%s})\n" author email))
658 (author (format "@author %s\n" author))
659 (email (format "@author @email{%s}\n" email))))
660 ;; Other authors.
661 (let ((subauthor (plist-get info :subauthor)))
662 (and subauthor
663 (org-element-normalize-string
664 (replace-regexp-in-string "^" "@author " subauthor))))))
665 (and copying "@page\n@vskip 0pt plus 1filll\n@insertcopying\n")
666 "@end titlepage\n\n"
667 ;; Table of contents.
668 (and (plist-get info :with-toc) "@contents\n\n")
669 ;; Configure Top Node when not for TeX. Also include contents
670 ;; from the first section of the document.
671 "@ifnottex\n"
672 "@node Top\n"
673 (format "@top %s\n" title)
674 (let* ((first-section
675 (org-element-map (plist-get info :parse-tree) 'section
676 #'identity info t '(headline)))
677 (top-contents
678 (org-export-data (org-element-contents first-section) info)))
679 (and (org-string-nw-p top-contents) (concat "\n" top-contents)))
680 "@end ifnottex\n\n"
681 ;; Menu.
682 (org-texinfo-make-menu (plist-get info :parse-tree) info 'master)
683 "\n"
684 ;; Document's body.
685 contents "\n"
686 ;; Creator.
687 (and (plist-get info :with-creator)
688 (concat (plist-get info :creator) "\n"))
689 ;; Document end.
690 "@bye")))
694 ;;; Transcode Functions
696 ;;;; Bold
698 (defun org-texinfo-bold (_bold contents info)
699 "Transcode BOLD from Org to Texinfo.
700 CONTENTS is the text with bold markup. INFO is a plist holding
701 contextual information."
702 (org-texinfo--text-markup contents 'bold info))
704 ;;;; Center Block
706 (defun org-texinfo-center-block (_center-block contents _info)
707 "Transcode a CENTER-BLOCK element from Org to Texinfo.
708 CONTENTS holds the contents of the block. INFO is a plist used
709 as a communication channel."
710 contents)
712 ;;;; Clock
714 (defun org-texinfo-clock (clock _contents info)
715 "Transcode a CLOCK element from Org to Texinfo.
716 CONTENTS is nil. INFO is a plist holding contextual
717 information."
718 (concat
719 "@noindent"
720 (format "@strong{%s} " org-clock-string)
721 (format (plist-get info :texinfo-inactive-timestamp-format)
722 (concat (org-timestamp-translate (org-element-property :value clock))
723 (let ((time (org-element-property :duration clock)))
724 (and time (format " (%s)" time)))))
725 "@*"))
727 ;;;; Code
729 (defun org-texinfo-code (code _contents info)
730 "Transcode a CODE object from Org to Texinfo.
731 CONTENTS is nil. INFO is a plist used as a communication
732 channel."
733 (org-texinfo--text-markup (org-element-property :value code) 'code info))
735 ;;;; Drawer
737 (defun org-texinfo-drawer (drawer contents info)
738 "Transcode a DRAWER element from Org to Texinfo.
739 CONTENTS holds the contents of the block. INFO is a plist
740 holding contextual information."
741 (let* ((name (org-element-property :drawer-name drawer))
742 (output (funcall (plist-get info :texinfo-format-drawer-function)
743 name contents)))
744 output))
746 ;;;; Dynamic Block
748 (defun org-texinfo-dynamic-block (_dynamic-block contents _info)
749 "Transcode a DYNAMIC-BLOCK element from Org to Texinfo.
750 CONTENTS holds the contents of the block. INFO is a plist
751 holding contextual information."
752 contents)
754 ;;;; Entity
756 (defun org-texinfo-entity (entity _contents _info)
757 "Transcode an ENTITY object from Org to Texinfo."
758 ;; Since there is not specific Texinfo entry in entities, use
759 ;; Texinfo-specific commands whenever possible, and fallback to
760 ;; UTF-8 otherwise.
761 (pcase (org-element-property :name entity)
762 ("AElig" "@AE{}")
763 ("aelig" "@ae{}")
764 ((or "bull" "bullet") "@bullet{}")
765 ("copy" "@copyright{}")
766 ("deg" "@textdegree{}")
767 ((or "dots" "hellip") "@dots{}")
768 ("equiv" "@equiv{}")
769 ((or "euro" "EUR") "@euro{}")
770 ((or "ge" "geq") "@geq{}")
771 ("laquo" "@guillemetleft{}")
772 ("iexcl" "@exclamdown{}")
773 ("imath" "@dotless{i}")
774 ("iquest" "@questiondown{}")
775 ("jmath" "@dotless{j}")
776 ((or "le" "leq") "@leq{}")
777 ("lsaquo" "@guilsinglleft{}")
778 ("mdash" "---")
779 ("minus" "@minus{}")
780 ("nbsp" "@tie{}")
781 ("ndash" "--")
782 ("OElig" "@OE{}")
783 ("oelig" "@oe{}")
784 ("ordf" "@ordf{}")
785 ("ordm" "@ordm{}")
786 ("pound" "@pound{}")
787 ("raquo" "@guillemetright{}")
788 ((or "rArr" "Rightarrow") "@result{}")
789 ("reg" "@registeredsymbol{}")
790 ((or "rightarrow" "to" "rarr") "@arrow{}")
791 ("rsaquo" "@guilsinglright{}")
792 ("thorn" "@th{}")
793 ("THORN" "@TH{}")
794 ((and (pred (string-prefix-p "_")) name) ;spacing entities
795 (format "@w{%s}" (substring name 1)))
796 (_ (org-element-property :utf-8 entity))))
798 ;;;; Example Block
800 (defun org-texinfo-example-block (example-block _contents info)
801 "Transcode an EXAMPLE-BLOCK element from Org to Texinfo.
802 CONTENTS is nil. INFO is a plist holding contextual
803 information."
804 (format "@example\n%s@end example"
805 (org-texinfo--sanitize-content
806 (org-export-format-code-default example-block info))))
808 ;;; Export Block
810 (defun org-texinfo-export-block (export-block _contents _info)
811 "Transcode a EXPORT-BLOCK element from Org to Texinfo.
812 CONTENTS is nil. INFO is a plist holding contextual information."
813 (when (string= (org-element-property :type export-block) "TEXINFO")
814 (org-remove-indentation (org-element-property :value export-block))))
816 ;;; Export Snippet
818 (defun org-texinfo-export-snippet (export-snippet _contents _info)
819 "Transcode a EXPORT-SNIPPET object from Org to Texinfo.
820 CONTENTS is nil. INFO is a plist holding contextual information."
821 (when (eq (org-export-snippet-backend export-snippet) 'texinfo)
822 (org-element-property :value export-snippet)))
824 ;;;; Fixed Width
826 (defun org-texinfo-fixed-width (fixed-width _contents _info)
827 "Transcode a FIXED-WIDTH element from Org to Texinfo.
828 CONTENTS is nil. INFO is a plist holding contextual information."
829 (format "@example\n%s@end example"
830 (org-remove-indentation
831 (org-texinfo--sanitize-content
832 (org-element-property :value fixed-width)))))
834 ;;;; Footnote Reference
836 (defun org-texinfo-footnote-reference (footnote _contents info)
837 "Create a footnote reference for FOOTNOTE.
839 FOOTNOTE is the footnote to define. CONTENTS is nil. INFO is a
840 plist holding contextual information."
841 (let ((def (org-export-get-footnote-definition footnote info)))
842 (format "@footnote{%s}"
843 (org-trim (org-export-data def info)))))
845 ;;;; Headline
847 (defun org-texinfo--structuring-command (headline info)
848 "Return Texinfo structuring command string for HEADLINE element.
849 Return nil if HEADLINE is to be ignored, `plain-list' if it
850 should be exported as a plain-list item. INFO is a plist holding
851 contextual information."
852 (cond
853 ((org-element-property :footnote-section-p headline) nil)
854 ((org-not-nil (org-export-get-node-property :COPYING headline t)) nil)
855 ((org-export-low-level-p headline info) 'plain-list)
857 (let ((class (plist-get info :texinfo-class)))
858 (pcase (assoc class (plist-get info :texinfo-classes))
859 (`(,_ ,_ . ,sections)
860 (pcase (nth (1- (org-export-get-relative-level headline info))
861 sections)
862 (`(,numbered ,unnumbered ,appendix)
863 (cond
864 ((org-not-nil (org-export-get-node-property :APPENDIX headline t))
865 appendix)
866 ((org-not-nil (org-export-get-node-property :INDEX headline t))
867 unnumbered)
868 ((org-export-numbered-headline-p headline info) numbered)
869 (t unnumbered)))
870 (`nil 'plain-list)
871 (_ (user-error "Invalid Texinfo class specification: %S" class))))
872 (_ (user-error "Invalid Texinfo class specification: %S" class)))))))
874 (defun org-texinfo-headline (headline contents info)
875 "Transcode a HEADLINE element from Org to Texinfo.
876 CONTENTS holds the contents of the headline. INFO is a plist
877 holding contextual information."
878 (let ((section-fmt (org-texinfo--structuring-command headline info)))
879 (when section-fmt
880 (let* ((todo
881 (and (plist-get info :with-todo-keywords)
882 (let ((todo (org-element-property :todo-keyword headline)))
883 (and todo (org-export-data todo info)))))
884 (todo-type (and todo (org-element-property :todo-type headline)))
885 (tags (and (plist-get info :with-tags)
886 (org-export-get-tags headline info)))
887 (priority (and (plist-get info :with-priority)
888 (org-element-property :priority headline)))
889 (text (org-texinfo--sanitize-title
890 (org-element-property :title headline) info))
891 (full-text
892 (funcall (plist-get info :texinfo-format-headline-function)
893 todo todo-type priority text tags))
894 (contents
895 (concat "\n"
896 (if (org-string-nw-p contents)
897 (concat "\n" contents)
899 (let ((index (org-element-property :INDEX headline)))
900 (and (member index '("cp" "fn" "ky" "pg" "tp" "vr"))
901 (format "\n@printindex %s\n" index))))))
902 (cond
903 ((eq section-fmt 'plain-list)
904 (let ((numbered? (org-export-numbered-headline-p headline info)))
905 (concat (and (org-export-first-sibling-p headline info)
906 (format "@%s\n" (if numbered? 'enumerate 'itemize)))
907 "@item\n" full-text "\n"
908 contents
909 (if (org-export-last-sibling-p headline info)
910 (format "@end %s" (if numbered? 'enumerate 'itemize))
911 "\n"))))
913 (concat (format "@node %s\n" (org-texinfo--get-node headline info))
914 (format section-fmt full-text)
915 contents)))))))
917 (defun org-texinfo-format-headline-default-function
918 (todo _todo-type priority text tags)
919 "Default format function for a headline.
920 See `org-texinfo-format-headline-function' for details."
921 (concat (when todo (format "@strong{%s} " todo))
922 (when priority (format "@emph{#%s} " priority))
923 text
924 (when tags (format " :%s:" (mapconcat 'identity tags ":")))))
926 ;;;; Inline Src Block
928 (defun org-texinfo-inline-src-block (inline-src-block _contents _info)
929 "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
930 CONTENTS holds the contents of the item. INFO is a plist holding
931 contextual information."
932 (format "@code{%s}"
933 (org-texinfo--sanitize-content
934 (org-element-property :value inline-src-block))))
936 ;;;; Inlinetask
938 (defun org-texinfo-inlinetask (inlinetask contents info)
939 "Transcode an INLINETASK element from Org to Texinfo.
940 CONTENTS holds the contents of the block. INFO is a plist
941 holding contextual information."
942 (let ((title (org-export-data (org-element-property :title inlinetask) info))
943 (todo (and (plist-get info :with-todo-keywords)
944 (let ((todo (org-element-property :todo-keyword inlinetask)))
945 (and todo (org-export-data todo info)))))
946 (todo-type (org-element-property :todo-type inlinetask))
947 (tags (and (plist-get info :with-tags)
948 (org-export-get-tags inlinetask info)))
949 (priority (and (plist-get info :with-priority)
950 (org-element-property :priority inlinetask))))
951 (funcall (plist-get info :texinfo-format-inlinetask-function)
952 todo todo-type priority title tags contents)))
954 (defun org-texinfo-format-inlinetask-default-function
955 (todo _todo-type priority title tags contents)
956 "Default format function for a inlinetasks.
957 See `org-texinfo-format-inlinetask-function' for details."
958 (let ((full-title
959 (concat (when todo (format "@strong{%s} " todo))
960 (when priority (format "#%c " priority))
961 title
962 (when tags (format ":%s:" (mapconcat #'identity tags ":"))))))
963 (format "@center %s\n\n%s\n" full-title contents)))
965 ;;;; Italic
967 (defun org-texinfo-italic (_italic contents info)
968 "Transcode ITALIC from Org to Texinfo.
969 CONTENTS is the text with italic markup. INFO is a plist holding
970 contextual information."
971 (org-texinfo--text-markup contents 'italic info))
973 ;;;; Item
975 (defun org-texinfo-item (item contents info)
976 "Transcode an ITEM element from Org to Texinfo.
977 CONTENTS holds the contents of the item. INFO is a plist holding
978 contextual information."
979 (let* ((tag (org-element-property :tag item))
980 (split (org-string-nw-p
981 (org-export-read-attribute :attr_texinfo
982 (org-element-property :parent item)
983 :sep)))
984 (items (and tag
985 (let ((tag (org-export-data tag info)))
986 (if split
987 (split-string tag (regexp-quote split) t "[ \t\n]+")
988 (list tag))))))
989 (format "%s\n%s"
990 (pcase items
991 (`nil "@item")
992 (`(,item) (concat "@item " item))
993 (`(,item . ,items)
994 (concat "@item " item "\n"
995 (mapconcat (lambda (i) (concat "@itemx " i))
996 items
997 "\n"))))
998 (or contents ""))))
1000 ;;;; Keyword
1002 (defun org-texinfo-keyword (keyword _contents info)
1003 "Transcode a KEYWORD element from Org to Texinfo.
1004 CONTENTS is nil. INFO is a plist holding contextual information."
1005 (let ((value (org-element-property :value keyword)))
1006 (pcase (org-element-property :key keyword)
1007 ("TEXINFO" value)
1008 ("CINDEX" (format "@cindex %s" value))
1009 ("FINDEX" (format "@findex %s" value))
1010 ("KINDEX" (format "@kindex %s" value))
1011 ("PINDEX" (format "@pindex %s" value))
1012 ("TINDEX" (format "@tindex %s" value))
1013 ("VINDEX" (format "@vindex %s" value))
1014 ("TOC"
1015 (cond ((string-match-p "\\<tables\\>" value)
1016 (concat "@listoffloats "
1017 (org-export-translate "Table" :utf-8 info)))
1018 ((string-match-p "\\<listings\\>" value)
1019 (concat "@listoffloats "
1020 (org-export-translate "Listing" :utf-8 info))))))))
1022 ;;;; Line Break
1024 (defun org-texinfo-line-break (_line-break _contents _info)
1025 "Transcode a LINE-BREAK object from Org to Texinfo.
1026 CONTENTS is nil. INFO is a plist holding contextual information."
1027 "@*\n")
1029 ;;;; Link
1031 (defun org-texinfo--@ref (datum description info)
1032 "Return @ref command for element or object DATUM.
1033 DESCRIPTION is the name of the section to print, as a string."
1034 (let ((node-name (org-texinfo--get-node datum info))
1035 ;; Sanitize DESCRIPTION for cross-reference use. In
1036 ;; particular, remove colons as they seem to cause (even
1037 ;; within @asis{...} to the Texinfo reader.
1038 (title (replace-regexp-in-string
1039 "[ \t]*:+" ""
1040 (replace-regexp-in-string "," "@comma{}" description))))
1041 (if (equal title node-name)
1042 (format "@ref{%s}" node-name)
1043 (format "@ref{%s, , %s}" node-name title))))
1045 (defun org-texinfo-link (link desc info)
1046 "Transcode a LINK object from Org to Texinfo.
1047 DESC is the description part of the link, or the empty string.
1048 INFO is a plist holding contextual information. See
1049 `org-export-data'."
1050 (let* ((type (org-element-property :type link))
1051 (raw-path (org-element-property :path link))
1052 ;; Ensure DESC really exists, or set it to nil.
1053 (desc (and (not (string= desc "")) desc))
1054 (path (cond
1055 ((member type '("http" "https" "ftp"))
1056 (concat type ":" raw-path))
1057 ((string= type "file") (org-export-file-uri raw-path))
1058 (t raw-path))))
1059 (cond
1060 ((org-export-custom-protocol-maybe link desc 'texinfo))
1061 ((org-export-inline-image-p link org-texinfo-inline-image-rules)
1062 (org-texinfo--inline-image link info))
1063 ((equal type "radio")
1064 (let ((destination (org-export-resolve-radio-link link info)))
1065 (if (not destination) desc
1066 (org-texinfo--@ref destination desc info))))
1067 ((member type '("custom-id" "id" "fuzzy"))
1068 (let ((destination
1069 (if (equal type "fuzzy")
1070 (org-export-resolve-fuzzy-link link info)
1071 (org-export-resolve-id-link link info))))
1072 (pcase (org-element-type destination)
1073 (`nil
1074 (format org-texinfo-link-with-unknown-path-format
1075 (org-texinfo--sanitize-content path)))
1076 ;; Id link points to an external file.
1077 (`plain-text
1078 (if desc (format "@uref{file://%s,%s}" destination desc)
1079 (format "@uref{file://%s}" destination)))
1080 ((or `headline
1081 ;; Targets within headlines cannot be turned into
1082 ;; @anchor{}, so we refer to the headline parent
1083 ;; directly.
1084 (and `target
1085 (guard (eq 'headline
1086 (org-element-type
1087 (org-element-property :parent destination))))))
1088 (let ((headline (org-element-lineage destination '(headline) t)))
1089 (org-texinfo--@ref
1090 headline
1091 (or desc (org-texinfo--sanitize-title
1092 (org-element-property :title headline) info))
1093 info)))
1095 (org-texinfo--@ref
1096 destination
1097 (or desc
1098 (pcase (org-export-get-ordinal destination info)
1099 ((and (pred integerp) n) (number-to-string n))
1100 ((and (pred consp) n) (mapconcat #'number-to-string n "."))
1101 (_ "???"))) ;cannot guess the description
1102 info)))))
1103 ((string= type "mailto")
1104 (format "@email{%s}"
1105 (concat (org-texinfo--sanitize-content path)
1106 (and desc (concat ", " desc)))))
1107 ;; External link with a description part.
1108 ((and path desc) (format "@uref{%s, %s}" path desc))
1109 ;; External link without a description part.
1110 (path (format "@uref{%s}" path))
1111 ;; No path, only description. Try to do something useful.
1113 (format (plist-get info :texinfo-link-with-unknown-path-format) desc)))))
1115 (defun org-texinfo--inline-image (link info)
1116 "Return Texinfo code for an inline image.
1117 LINK is the link pointing to the inline image. INFO is the
1118 current state of the export, as a plist."
1119 (let* ((parent (org-export-get-parent-element link))
1120 (label (and (org-element-property :name parent)
1121 (org-texinfo--get-node parent info)))
1122 (caption (org-export-get-caption parent))
1123 (shortcaption (org-export-get-caption parent t))
1124 (path (org-element-property :path link))
1125 (filename
1126 (file-name-sans-extension
1127 (if (file-name-absolute-p path) (expand-file-name path) path)))
1128 (extension (file-name-extension path))
1129 (attributes (org-export-read-attribute :attr_texinfo parent))
1130 (height (or (plist-get attributes :height) ""))
1131 (width (or (plist-get attributes :width) ""))
1132 (alt (or (plist-get attributes :alt) ""))
1133 (image (format "@image{%s,%s,%s,%s,%s}"
1134 filename width height alt extension)))
1135 (cond ((or caption shortcaption)
1136 (org-texinfo--wrap-float image
1137 info
1138 (org-export-translate "Figure" :utf-8 info)
1139 label
1140 caption
1141 shortcaption))
1142 (label (concat "@anchor{" label "}\n" image))
1143 (t image))))
1146 ;;;; Menu
1148 (defun org-texinfo-make-menu (scope info &optional master)
1149 "Create the menu for inclusion in the Texinfo document.
1151 SCOPE is a headline or a full parse tree. INFO is the
1152 communication channel, as a plist.
1154 When optional argument MASTER is non-nil, generate a master menu,
1155 including detailed node listing."
1156 (let ((menu (org-texinfo--build-menu scope info)))
1157 (when (org-string-nw-p menu)
1158 (org-element-normalize-string
1159 (format
1160 "@menu\n%s@end menu"
1161 (concat menu
1162 (when master
1163 (let ((detailmenu
1164 (org-texinfo--build-menu
1165 scope info
1166 (let ((toc-depth (plist-get info :with-toc)))
1167 (if (wholenump toc-depth) toc-depth
1168 org-texinfo-max-toc-depth)))))
1169 (when (org-string-nw-p detailmenu)
1170 (concat "\n@detailmenu\n"
1171 "--- The Detailed Node Listing ---\n\n"
1172 detailmenu
1173 "@end detailmenu\n"))))))))))
1175 (defun org-texinfo--build-menu (scope info &optional level)
1176 "Build menu for entries within SCOPE.
1177 SCOPE is a headline or a full parse tree. INFO is a plist
1178 containing contextual information. When optional argument LEVEL
1179 is an integer, build the menu recursively, down to this depth."
1180 (cond
1181 ((not level)
1182 (org-texinfo--format-entries (org-texinfo--menu-entries scope info) info))
1183 ((zerop level) nil)
1185 (org-element-normalize-string
1186 (mapconcat
1187 (lambda (h)
1188 (let ((entries (org-texinfo--menu-entries h info)))
1189 (when entries
1190 (concat
1191 (format "%s\n\n%s\n"
1192 (org-export-data (org-export-get-alt-title h info) info)
1193 (org-texinfo--format-entries entries info))
1194 (org-texinfo--build-menu h info (1- level))))))
1195 (org-texinfo--menu-entries scope info) "\n")))))
1197 (defun org-texinfo--format-entries (entries info)
1198 "Format all direct menu entries in SCOPE, as a string.
1199 SCOPE is either a headline or a full Org document. INFO is
1200 a plist containing contextual information."
1201 (org-element-normalize-string
1202 (mapconcat
1203 (lambda (h)
1204 (let* ((title
1205 ;; Colons are used as a separator between title and node
1206 ;; name. Remove them.
1207 (replace-regexp-in-string
1208 "[ \t]+:+" ""
1209 (org-texinfo--sanitize-title
1210 (org-export-get-alt-title h info) info)))
1211 (node (org-texinfo--get-node h info))
1212 (entry (concat "* " title ":"
1213 (if (string= title node) ":"
1214 (concat " " node ". "))))
1215 (desc (org-element-property :DESCRIPTION h)))
1216 (if (not desc) entry
1217 (format (format "%%-%ds %%s" org-texinfo-node-description-column)
1218 entry desc))))
1219 entries "\n")))
1221 (defun org-texinfo--menu-entries (scope info)
1222 "List direct children in SCOPE needing a menu entry.
1223 SCOPE is a headline or a full parse tree. INFO is a plist
1224 holding contextual information."
1225 (let* ((cache (or (plist-get info :texinfo-entries-cache)
1226 (plist-get (plist-put info :texinfo-entries-cache
1227 (make-hash-table :test #'eq))
1228 :texinfo-entries-cache)))
1229 (cached-entries (gethash scope cache 'no-cache)))
1230 (if (not (eq cached-entries 'no-cache)) cached-entries
1231 (puthash scope
1232 (cl-remove-if
1233 (lambda (h)
1234 (org-not-nil (org-export-get-node-property :COPYING h t)))
1235 (org-export-collect-headlines info 1 scope))
1236 cache))))
1238 ;;;; Node Property
1240 (defun org-texinfo-node-property (node-property _contents _info)
1241 "Transcode a NODE-PROPERTY element from Org to Texinfo.
1242 CONTENTS is nil. INFO is a plist holding contextual
1243 information."
1244 (format "%s:%s"
1245 (org-element-property :key node-property)
1246 (let ((value (org-element-property :value node-property)))
1247 (if value (concat " " value) ""))))
1249 ;;;; Paragraph
1251 (defun org-texinfo-paragraph (_paragraph contents _info)
1252 "Transcode a PARAGRAPH element from Org to Texinfo.
1253 CONTENTS is the contents of the paragraph, as a string. INFO is
1254 the plist used as a communication channel."
1255 contents)
1257 ;;;; Plain List
1259 (defun org-texinfo-plain-list (plain-list contents info)
1260 "Transcode a PLAIN-LIST element from Org to Texinfo.
1261 CONTENTS is the contents of the list. INFO is a plist holding
1262 contextual information."
1263 (let* ((attr (org-export-read-attribute :attr_texinfo plain-list))
1264 (indic (let ((i (or (plist-get attr :indic)
1265 (plist-get info :texinfo-table-default-markup))))
1266 ;; Allow indicating commands with missing @ sign.
1267 (if (string-prefix-p "@" i) i (concat "@" i))))
1268 (table-type (plist-get attr :table-type))
1269 (type (org-element-property :type plain-list))
1270 (list-type (cond
1271 ((eq type 'ordered) "enumerate")
1272 ((eq type 'unordered) "itemize")
1273 ((member table-type '("ftable" "vtable")) table-type)
1274 (t "table"))))
1275 (format "@%s\n%s@end %s"
1276 (if (eq type 'descriptive) (concat list-type " " indic) list-type)
1277 contents
1278 list-type)))
1280 ;;;; Plain Text
1282 (defun org-texinfo-plain-text (text info)
1283 "Transcode a TEXT string from Org to Texinfo.
1284 TEXT is the string to transcode. INFO is a plist holding
1285 contextual information."
1286 ;; First protect @, { and }.
1287 (let ((output (org-texinfo--sanitize-content text)))
1288 ;; Activate smart quotes. Be sure to provide original TEXT string
1289 ;; since OUTPUT may have been modified.
1290 (when (plist-get info :with-smart-quotes)
1291 (setq output
1292 (org-export-activate-smart-quotes output :texinfo info text)))
1293 ;; LaTeX into @LaTeX{} and TeX into @TeX{}
1294 (let ((case-fold-search nil))
1295 (setq output (replace-regexp-in-string "\\(?:La\\)?TeX" "@\\&{}" output)))
1296 ;; Convert special strings.
1297 (when (plist-get info :with-special-strings)
1298 (setq output
1299 (replace-regexp-in-string
1300 "\\.\\.\\." "@dots{}"
1301 (replace-regexp-in-string "\\\\-" "@-" output))))
1302 ;; Handle break preservation if required.
1303 (when (plist-get info :preserve-breaks)
1304 (setq output (replace-regexp-in-string
1305 "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" output)))
1306 ;; Return value.
1307 output))
1309 ;;;; Planning
1311 (defun org-texinfo-planning (planning _contents info)
1312 "Transcode a PLANNING element from Org to Texinfo.
1313 CONTENTS is nil. INFO is a plist holding contextual
1314 information."
1315 (concat
1316 "@noindent"
1317 (mapconcat
1318 'identity
1319 (delq nil
1320 (list
1321 (let ((closed (org-element-property :closed planning)))
1322 (when closed
1323 (concat
1324 (format "@strong{%s} " org-closed-string)
1325 (format (plist-get info :texinfo-inactive-timestamp-format)
1326 (org-timestamp-translate closed)))))
1327 (let ((deadline (org-element-property :deadline planning)))
1328 (when deadline
1329 (concat
1330 (format "@strong{%s} " org-deadline-string)
1331 (format (plist-get info :texinfo-active-timestamp-format)
1332 (org-timestamp-translate deadline)))))
1333 (let ((scheduled (org-element-property :scheduled planning)))
1334 (when scheduled
1335 (concat
1336 (format "@strong{%s} " org-scheduled-string)
1337 (format (plist-get info :texinfo-active-timestamp-format)
1338 (org-timestamp-translate scheduled)))))))
1339 " ")
1340 "@*"))
1342 ;;;; Property Drawer
1344 (defun org-texinfo-property-drawer (_property-drawer contents _info)
1345 "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
1346 CONTENTS holds the contents of the drawer. INFO is a plist
1347 holding contextual information."
1348 (and (org-string-nw-p contents)
1349 (format "@verbatim\n%s@end verbatim" contents)))
1351 ;;;; Quote Block
1353 (defun org-texinfo-quote-block (quote-block contents _info)
1354 "Transcode a QUOTE-BLOCK element from Org to Texinfo.
1355 CONTENTS holds the contents of the block. INFO is a plist
1356 holding contextual information."
1357 (let* ((title (org-element-property :name quote-block))
1358 (start-quote (concat "@quotation"
1359 (if title
1360 (format " %s" title)))))
1361 (format "%s\n%s@end quotation" start-quote contents)))
1363 ;;;; Radio Target
1365 (defun org-texinfo-radio-target (radio-target text info)
1366 "Transcode a RADIO-TARGET object from Org to Texinfo.
1367 TEXT is the text of the target. INFO is a plist holding
1368 contextual information."
1369 (format "@anchor{%s}%s"
1370 (org-texinfo--get-node radio-target info)
1371 text))
1373 ;;;; Section
1375 (defun org-texinfo-section (section contents info)
1376 "Transcode a SECTION element from Org to Texinfo.
1377 CONTENTS holds the contents of the section. INFO is a plist
1378 holding contextual information."
1379 (let ((parent (org-export-get-parent-headline section)))
1380 (when parent ;ignore very first section
1381 (org-trim
1382 (concat contents "\n" (org-texinfo-make-menu parent info))))))
1384 ;;;; Special Block
1386 (defun org-texinfo-special-block (special-block contents _info)
1387 "Transcode a SPECIAL-BLOCK element from Org to Texinfo.
1388 CONTENTS holds the contents of the block. INFO is a plist used
1389 as a communication channel."
1390 (let ((opt (org-export-read-attribute :attr_texinfo special-block :options))
1391 (type (org-element-property :type special-block)))
1392 (format "@%s%s\n%s@end %s"
1393 type
1394 (if opt (concat " " opt) "")
1395 (or contents "")
1396 type)))
1398 ;;;; Src Block
1400 (defun org-texinfo-src-block (src-block _contents info)
1401 "Transcode a SRC-BLOCK element from Org to Texinfo.
1402 CONTENTS holds the contents of the item. INFO is a plist holding
1403 contextual information."
1404 (let* ((lisp (string-match-p "lisp"
1405 (org-element-property :language src-block)))
1406 (code (org-texinfo--sanitize-content
1407 (org-export-format-code-default src-block info)))
1408 (value (format
1409 (if lisp "@lisp\n%s@end lisp" "@example\n%s@end example")
1410 code))
1411 (caption (org-export-get-caption src-block))
1412 (shortcaption (org-export-get-caption src-block t)))
1413 (if (not (or caption shortcaption)) value
1414 (org-texinfo--wrap-float value
1415 info
1416 (org-export-translate "Listing" :utf-8 info)
1417 (org-texinfo--get-node src-block info)
1418 caption
1419 shortcaption))))
1421 ;;;; Statistics Cookie
1423 (defun org-texinfo-statistics-cookie (statistics-cookie _contents _info)
1424 "Transcode a STATISTICS-COOKIE object from Org to Texinfo.
1425 CONTENTS is nil. INFO is a plist holding contextual information."
1426 (org-element-property :value statistics-cookie))
1429 ;;;; Strike-through
1431 (defun org-texinfo-strike-through (_strike-through contents info)
1432 "Transcode STRIKE-THROUGH from Org to Texinfo.
1433 CONTENTS is the text with strike-through markup. INFO is a plist
1434 holding contextual information."
1435 (org-texinfo--text-markup contents 'strike-through info))
1437 ;;;; Subscript
1439 (defun org-texinfo-subscript (_subscript contents _info)
1440 "Transcode a SUBSCRIPT object from Org to Texinfo.
1441 CONTENTS is the contents of the object. INFO is a plist holding
1442 contextual information."
1443 (format "@math{_%s}" contents))
1445 ;;;; Superscript
1447 (defun org-texinfo-superscript (_superscript contents _info)
1448 "Transcode a SUPERSCRIPT object from Org to Texinfo.
1449 CONTENTS is the contents of the object. INFO is a plist holding
1450 contextual information."
1451 (format "@math{^%s}" contents))
1453 ;;;; Table
1455 (defun org-texinfo-table (table contents info)
1456 "Transcode a TABLE element from Org to Texinfo.
1457 CONTENTS is the contents of the table. INFO is a plist holding
1458 contextual information."
1459 (if (eq (org-element-property :type table) 'table.el)
1460 (format "@verbatim\n%s@end verbatim"
1461 (org-element-normalize-string
1462 (org-element-property :value table)))
1463 (let* ((col-width (org-export-read-attribute :attr_texinfo table :columns))
1464 (columns
1465 (if col-width (format "@columnfractions %s" col-width)
1466 (org-texinfo-table-column-widths table info)))
1467 (caption (org-export-get-caption table))
1468 (shortcaption (org-export-get-caption table t))
1469 (table-str (format "@multitable %s\n%s@end multitable"
1470 columns
1471 contents)))
1472 (if (not (or caption shortcaption)) table-str
1473 (org-texinfo--wrap-float table-str
1474 info
1475 (org-export-translate "Table" :utf-8 info)
1476 (org-texinfo--get-node table info)
1477 caption
1478 shortcaption)))))
1480 (defun org-texinfo-table-column-widths (table info)
1481 "Determine the largest table cell in each column to process alignment.
1482 TABLE is the table element to transcode. INFO is a plist used as
1483 a communication channel."
1484 (let ((widths (make-vector (cdr (org-export-table-dimensions table info)) 0)))
1485 (org-element-map table 'table-row
1486 (lambda (row)
1487 (let ((idx 0))
1488 (org-element-map row 'table-cell
1489 (lambda (cell)
1490 ;; Length of the cell in the original buffer is only an
1491 ;; approximation of the length of the cell in the
1492 ;; output. It can sometimes fail (e.g. it considers
1493 ;; "/a/" being larger than "ab").
1494 (let ((w (- (org-element-property :contents-end cell)
1495 (org-element-property :contents-begin cell))))
1496 (aset widths idx (max w (aref widths idx))))
1497 (cl-incf idx))
1498 info)))
1499 info)
1500 (format "{%s}" (mapconcat (lambda (w) (make-string w ?a)) widths "} {"))))
1502 ;;;; Table Cell
1504 (defun org-texinfo-table-cell (table-cell contents info)
1505 "Transcode a TABLE-CELL element from Org to Texinfo.
1506 CONTENTS is the cell contents. INFO is a plist used as
1507 a communication channel."
1508 (concat
1509 (let ((scientific-notation
1510 (plist-get info :texinfo-table-scientific-notation)))
1511 (if (and contents
1512 scientific-notation
1513 (string-match orgtbl-exp-regexp contents))
1514 ;; Use appropriate format string for scientific notation.
1515 (format scientific-notation
1516 (match-string 1 contents)
1517 (match-string 2 contents))
1518 contents))
1519 (when (org-export-get-next-element table-cell info) "\n@tab ")))
1521 ;;;; Table Row
1523 (defun org-texinfo-table-row (table-row contents info)
1524 "Transcode a TABLE-ROW element from Org to Texinfo.
1525 CONTENTS is the contents of the row. INFO is a plist used as
1526 a communication channel."
1527 ;; Rules are ignored since table separators are deduced from
1528 ;; borders of the current row.
1529 (when (eq (org-element-property :type table-row) 'standard)
1530 (let ((rowgroup-tag
1531 (if (and (= 1 (org-export-table-row-group table-row info))
1532 (org-export-table-has-header-p
1533 (org-export-get-parent-table table-row) info))
1534 "@headitem "
1535 "@item ")))
1536 (concat rowgroup-tag contents "\n"))))
1538 ;;;; Target
1540 (defun org-texinfo-target (target _contents info)
1541 "Transcode a TARGET object from Org to Texinfo.
1542 CONTENTS is nil. INFO is a plist holding contextual
1543 information."
1544 (format "@anchor{%s}" (org-texinfo--get-node target info)))
1546 ;;;; Timestamp
1548 (defun org-texinfo-timestamp (timestamp _contents info)
1549 "Transcode a TIMESTAMP object from Org to Texinfo.
1550 CONTENTS is nil. INFO is a plist holding contextual
1551 information."
1552 (let ((value (org-texinfo-plain-text
1553 (org-timestamp-translate timestamp) info)))
1554 (pcase (org-element-property :type timestamp)
1555 ((or `active `active-range)
1556 (format (plist-get info :texinfo-active-timestamp-format) value))
1557 ((or `inactive `inactive-range)
1558 (format (plist-get info :texinfo-inactive-timestamp-format) value))
1559 (_ (format (plist-get info :texinfo-diary-timestamp-format) value)))))
1561 ;;;; Underline
1563 (defun org-texinfo-underline (_underline contents info)
1564 "Transcode UNDERLINE from Org to Texinfo.
1565 CONTENTS is the text with underline markup. INFO is a plist
1566 holding contextual information."
1567 (org-texinfo--text-markup contents 'underline info))
1569 ;;;; Verbatim
1571 (defun org-texinfo-verbatim (verbatim _contents info)
1572 "Transcode a VERBATIM object from Org to Texinfo.
1573 CONTENTS is nil. INFO is a plist used as a communication
1574 channel."
1575 (org-texinfo--text-markup
1576 (org-element-property :value verbatim) 'verbatim info))
1578 ;;;; Verse Block
1580 (defun org-texinfo-verse-block (_verse-block contents _info)
1581 "Transcode a VERSE-BLOCK element from Org to Texinfo.
1582 CONTENTS is verse block contents. INFO is a plist holding
1583 contextual information."
1584 (format "@display\n%s@end display" contents))
1587 ;;; Interactive functions
1589 ;;;###autoload
1590 (defun org-texinfo-export-to-texinfo
1591 (&optional async subtreep visible-only body-only ext-plist)
1592 "Export current buffer to a Texinfo file.
1594 If narrowing is active in the current buffer, only export its
1595 narrowed part.
1597 If a region is active, export that region.
1599 A non-nil optional argument ASYNC means the process should happen
1600 asynchronously. The resulting file should be accessible through
1601 the `org-export-stack' interface.
1603 When optional argument SUBTREEP is non-nil, export the sub-tree
1604 at point, extracting information from the headline properties
1605 first.
1607 When optional argument VISIBLE-ONLY is non-nil, don't export
1608 contents of hidden elements.
1610 When optional argument BODY-ONLY is non-nil, only write code
1611 between \"\\begin{document}\" and \"\\end{document}\".
1613 EXT-PLIST, when provided, is a property list with external
1614 parameters overriding Org default settings, but still inferior to
1615 file-local settings.
1617 Return output file's name."
1618 (interactive)
1619 (let ((outfile (org-export-output-file-name ".texi" subtreep))
1620 (org-export-coding-system org-texinfo-coding-system))
1621 (org-export-to-file 'texinfo outfile
1622 async subtreep visible-only body-only ext-plist)))
1624 ;;;###autoload
1625 (defun org-texinfo-export-to-info
1626 (&optional async subtreep visible-only body-only ext-plist)
1627 "Export current buffer to Texinfo then process through to INFO.
1629 If narrowing is active in the current buffer, only export its
1630 narrowed part.
1632 If a region is active, export that region.
1634 A non-nil optional argument ASYNC means the process should happen
1635 asynchronously. The resulting file should be accessible through
1636 the `org-export-stack' interface.
1638 When optional argument SUBTREEP is non-nil, export the sub-tree
1639 at point, extracting information from the headline properties
1640 first.
1642 When optional argument VISIBLE-ONLY is non-nil, don't export
1643 contents of hidden elements.
1645 When optional argument BODY-ONLY is non-nil, only write code
1646 between \"\\begin{document}\" and \"\\end{document}\".
1648 EXT-PLIST, when provided, is a property list with external
1649 parameters overriding Org default settings, but still inferior to
1650 file-local settings.
1652 When optional argument PUB-DIR is set, use it as the publishing
1653 directory.
1655 Return INFO file's name."
1656 (interactive)
1657 (let ((outfile (org-export-output-file-name ".texi" subtreep))
1658 (org-export-coding-system org-texinfo-coding-system))
1659 (org-export-to-file 'texinfo outfile
1660 async subtreep visible-only body-only ext-plist
1661 (lambda (file) (org-texinfo-compile file)))))
1663 ;;;###autoload
1664 (defun org-texinfo-publish-to-texinfo (plist filename pub-dir)
1665 "Publish an org file to Texinfo.
1667 FILENAME is the filename of the Org file to be published. PLIST
1668 is the property list for the given project. PUB-DIR is the
1669 publishing directory.
1671 Return output file name."
1672 (org-publish-org-to 'texinfo filename ".texi" plist pub-dir))
1674 ;;;###autoload
1675 (defun org-texinfo-convert-region-to-texinfo ()
1676 "Assume the current region has Org syntax, and convert it to Texinfo.
1677 This can be used in any buffer. For example, you can write an
1678 itemized list in Org syntax in an Texinfo buffer and use this
1679 command to convert it."
1680 (interactive)
1681 (org-export-replace-region-by 'texinfo))
1683 (defun org-texinfo-compile (file)
1684 "Compile a texinfo file.
1686 FILE is the name of the file being compiled. Processing is done
1687 through the command specified in `org-texinfo-info-process',
1688 which see. Output is redirected to \"*Org INFO Texinfo Output*\"
1689 buffer.
1691 Return INFO file name or an error if it couldn't be produced."
1692 (message "Processing Texinfo file %s..." file)
1693 (let* ((log-name "*Org INFO Texinfo Output*")
1694 (log (get-buffer-create log-name))
1695 (output
1696 (org-compile-file file org-texinfo-info-process "info"
1697 (format "See %S for details" log-name)
1698 log)))
1699 (when org-texinfo-remove-logfiles
1700 (let ((base (file-name-sans-extension output)))
1701 (dolist (ext org-texinfo-logfiles-extensions)
1702 (let ((file (concat base "." ext)))
1703 (when (file-exists-p file) (delete-file file))))))
1704 (message "Process completed.")
1705 output))
1708 (provide 'ox-texinfo)
1710 ;; Local variables:
1711 ;; generated-autoload-file: "org-loaddefs.el"
1712 ;; End:
1714 ;;; ox-texinfo.el ends here