Merge branch 'maint'
[org-mode.git] / lisp / ox-texinfo.el
blobd60fa42429f6879d9a26c497858cbb63e594ed0a
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 <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;; See Org manual for details.
26 ;;; Code:
28 (require 'cl-lib)
29 (require 'ox)
31 (defvar orgtbl-exp-regexp)
35 ;;; Define Back-End
37 (org-export-define-backend 'texinfo
38 '((bold . org-texinfo-bold)
39 (center-block . org-texinfo-center-block)
40 (clock . org-texinfo-clock)
41 (code . org-texinfo-code)
42 (drawer . org-texinfo-drawer)
43 (dynamic-block . org-texinfo-dynamic-block)
44 (entity . org-texinfo-entity)
45 (example-block . org-texinfo-example-block)
46 (export-block . org-texinfo-export-block)
47 (export-snippet . org-texinfo-export-snippet)
48 (fixed-width . org-texinfo-fixed-width)
49 (footnote-definition . org-texinfo-footnote-definition)
50 (footnote-reference . org-texinfo-footnote-reference)
51 (headline . org-texinfo-headline)
52 (inline-src-block . org-texinfo-inline-src-block)
53 (inlinetask . org-texinfo-inlinetask)
54 (italic . org-texinfo-italic)
55 (item . org-texinfo-item)
56 (keyword . org-texinfo-keyword)
57 (line-break . org-texinfo-line-break)
58 (link . org-texinfo-link)
59 (node-property . org-texinfo-node-property)
60 (paragraph . org-texinfo-paragraph)
61 (plain-list . org-texinfo-plain-list)
62 (plain-text . org-texinfo-plain-text)
63 (planning . org-texinfo-planning)
64 (property-drawer . org-texinfo-property-drawer)
65 (quote-block . org-texinfo-quote-block)
66 (radio-target . org-texinfo-radio-target)
67 (section . org-texinfo-section)
68 (special-block . org-texinfo-special-block)
69 (src-block . org-texinfo-src-block)
70 (statistics-cookie . org-texinfo-statistics-cookie)
71 (strike-through . org-texinfo-strike-through)
72 (subscript . org-texinfo-subscript)
73 (superscript . org-texinfo-superscript)
74 (table . org-texinfo-table)
75 (table-cell . org-texinfo-table-cell)
76 (table-row . org-texinfo-table-row)
77 (target . org-texinfo-target)
78 (template . org-texinfo-template)
79 (timestamp . org-texinfo-timestamp)
80 (underline . org-texinfo-underline)
81 (verbatim . org-texinfo-verbatim)
82 (verse-block . org-texinfo-verse-block))
83 :filters-alist
84 '((:filter-headline . org-texinfo--filter-section-blank-lines)
85 (:filter-parse-tree . org-texinfo--normalize-headlines)
86 (:filter-section . org-texinfo--filter-section-blank-lines))
87 :menu-entry
88 '(?i "Export to Texinfo"
89 ((?t "As TEXI file" org-texinfo-export-to-texinfo)
90 (?i "As INFO file" org-texinfo-export-to-info)
91 (?o "As INFO file and open"
92 (lambda (a s v b)
93 (if a (org-texinfo-export-to-info t s v b)
94 (org-open-file (org-texinfo-export-to-info nil s v b)))))))
95 :options-alist
96 '((:texinfo-filename "TEXINFO_FILENAME" nil nil t)
97 (:texinfo-class "TEXINFO_CLASS" nil org-texinfo-default-class t)
98 (:texinfo-header "TEXINFO_HEADER" nil nil newline)
99 (:texinfo-post-header "TEXINFO_POST_HEADER" nil nil newline)
100 (:subtitle "SUBTITLE" nil nil parse)
101 (:subauthor "SUBAUTHOR" nil nil newline)
102 (:texinfo-dircat "TEXINFO_DIR_CATEGORY" nil nil t)
103 (:texinfo-dirtitle "TEXINFO_DIR_TITLE" nil nil t)
104 (:texinfo-dirdesc "TEXINFO_DIR_DESC" nil nil t)
105 (:texinfo-printed-title "TEXINFO_PRINTED_TITLE" nil nil t)
106 ;; Other variables.
107 (:texinfo-classes nil nil org-texinfo-classes)
108 (:texinfo-format-headline-function nil nil org-texinfo-format-headline-function)
109 (:texinfo-node-description-column nil nil org-texinfo-node-description-column)
110 (:texinfo-active-timestamp-format nil nil org-texinfo-active-timestamp-format)
111 (:texinfo-inactive-timestamp-format nil nil org-texinfo-inactive-timestamp-format)
112 (:texinfo-diary-timestamp-format nil nil org-texinfo-diary-timestamp-format)
113 (:texinfo-link-with-unknown-path-format nil nil org-texinfo-link-with-unknown-path-format)
114 (:texinfo-tables-verbatim nil nil org-texinfo-tables-verbatim)
115 (:texinfo-table-scientific-notation nil nil org-texinfo-table-scientific-notation)
116 (:texinfo-def-table-markup nil nil org-texinfo-def-table-markup)
117 (:texinfo-text-markup-alist nil nil org-texinfo-text-markup-alist)
118 (:texinfo-format-drawer-function nil nil org-texinfo-format-drawer-function)
119 (:texinfo-format-inlinetask-function nil nil org-texinfo-format-inlinetask-function)))
123 ;;; User Configurable Variables
125 (defgroup org-export-texinfo nil
126 "Options for exporting Org mode files to Texinfo."
127 :tag "Org Export Texinfo"
128 :version "24.4"
129 :package-version '(Org . "8.0")
130 :group 'org-export)
132 ;;;; Preamble
134 (defcustom org-texinfo-coding-system nil
135 "Default document encoding for Texinfo output.
137 If nil it will default to `buffer-file-coding-system'."
138 :group 'org-export-texinfo
139 :type 'coding-system)
141 (defcustom org-texinfo-default-class "info"
142 "The default Texinfo class."
143 :group 'org-export-texinfo
144 :type '(string :tag "Texinfo class"))
146 (defcustom org-texinfo-classes
147 '(("info"
148 "@documentencoding AUTO\n@documentlanguage AUTO"
149 ("@chapter %s" . "@unnumbered %s")
150 ("@section %s" . "@unnumberedsec %s")
151 ("@subsection %s" . "@unnumberedsubsec %s")
152 ("@subsubsection %s" . "@unnumberedsubsubsec %s")))
153 "Alist of Texinfo classes and associated header and structure.
154 If #+TEXINFO_CLASS is set in the buffer, use its value and the
155 associated information. Here is the structure of each cell:
157 (class-name
158 header-string
159 (numbered-section . unnumbered-section)
160 ...)
163 The header string
164 -----------------
166 The header string is inserted in the header of the generated
167 document, right after \"@setfilename\" and \"@settitle\"
168 commands.
170 If it contains the special string
172 \"@documentencoding AUTO\"
174 \"AUTO\" will be replaced with an appropriate coding system. See
175 `org-texinfo-coding-system' for more information. Likewise, if
176 the string contains the special string
178 \"@documentlanguage AUTO\"
180 \"AUTO\" will be replaced with the language defined in the
181 buffer, through #+LANGUAGE keyword, or globally, with
182 `org-export-default-language', which see.
185 The sectioning structure
186 ------------------------
188 The sectioning structure of the class is given by the elements
189 following the header string. For each sectioning level, a number
190 of strings is specified. A %s formatter is mandatory in each
191 section string and will be replaced by the title of the section.
193 Instead of a list of sectioning commands, you can also specify
194 a function name. That function will be called with two
195 parameters, the reduced) level of the headline, and a predicate
196 non-nil when the headline should be numbered. It must return
197 a format string in which the section title will be added."
198 :group 'org-export-texinfo
199 :version "24.4"
200 :package-version '(Org . "8.2")
201 :type '(repeat
202 (list (string :tag "Texinfo class")
203 (string :tag "Texinfo header")
204 (repeat :tag "Levels" :inline t
205 (choice
206 (cons :tag "Heading"
207 (string :tag " numbered")
208 (string :tag "unnumbered"))
209 (function :tag "Hook computing sectioning"))))))
211 ;;;; Headline
213 (defcustom org-texinfo-format-headline-function
214 'org-texinfo-format-headline-default-function
215 "Function to format headline text.
217 This function will be called with 5 arguments:
218 TODO the todo keyword (string or nil).
219 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
220 PRIORITY the priority of the headline (integer or nil)
221 TEXT the main headline text (string).
222 TAGS the tags as a list of strings (list of strings or nil).
224 The function result will be used in the section format string."
225 :group 'org-export-texinfo
226 :type 'function
227 :version "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-def-table-markup "@samp"
283 "Default markup for first column in two-column tables.
285 This should an indicating command, e.g., \"@code\", \"@kbd\" or
286 \"@asis\".
288 It can be overridden locally using the \":indic\" attribute."
289 :group 'org-export-texinfo
290 :type 'string)
292 ;;;; Text markup
294 (defcustom org-texinfo-text-markup-alist '((bold . "@strong{%s}")
295 (code . code)
296 (italic . "@emph{%s}")
297 (verbatim . samp))
298 "Alist of Texinfo expressions to convert text markup.
300 The key must be a symbol among `bold', `code', `italic',
301 `strike-through', `underscore' and `verbatim'. The value is
302 a formatting string to wrap fontified text with.
304 Value can also be set to the following symbols: `verb', `samp'
305 and `code'. With the first one, Org uses \"@verb\" to create
306 a format string and selects a delimiter character that isn't in
307 the string. For the other two, Org uses \"@samp\" or \"@code\"
308 to typeset and protects special characters.
310 When no association is found for a given markup, text is returned
311 as-is."
312 :group 'org-export-texinfo
313 :version "26.1"
314 :package-version '(Org . "9.1")
315 :type 'alist
316 :options '(bold code italic strike-through underscore verbatim))
318 ;;;; Drawers
320 (defcustom org-texinfo-format-drawer-function (lambda (_name contents) contents)
321 "Function called to format a drawer in Texinfo code.
323 The function must accept two parameters:
324 NAME the drawer name, like \"LOGBOOK\"
325 CONTENTS the contents of the drawer.
327 The function should return the string to be exported.
329 The default function simply returns the value of CONTENTS."
330 :group 'org-export-texinfo
331 :version "24.4"
332 :package-version '(Org . "8.2")
333 :type 'function)
335 ;;;; Inlinetasks
337 (defcustom org-texinfo-format-inlinetask-function
338 'org-texinfo-format-inlinetask-default-function
339 "Function called to format an inlinetask in Texinfo code.
341 The function must accept six parameters:
342 TODO the todo keyword, as a string
343 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
344 PRIORITY the inlinetask priority, as a string
345 NAME the inlinetask name, as a string.
346 TAGS the inlinetask tags, as a list of strings.
347 CONTENTS the contents of the inlinetask, as a string.
349 The function should return the string to be exported."
350 :group 'org-export-texinfo
351 :type 'function)
353 ;;;; Compilation
355 (defcustom org-texinfo-info-process '("makeinfo %f")
356 "Commands to process a Texinfo file to an INFO file.
358 This is a list of strings, each of them will be given to the
359 shell as a command. %f in the command will be replaced by the
360 relative file name, %F by the absolute file name, %b by the file
361 base name (i.e. without directory and extension parts), %o by the
362 base directory of the file and %O by the absolute file name of
363 the output file."
364 :group 'org-export-texinfo
365 :type '(repeat :tag "Shell command sequence"
366 (string :tag "Shell command")))
368 (defcustom org-texinfo-logfiles-extensions
369 '("aux" "toc" "cp" "fn" "ky" "pg" "tp" "vr")
370 "The list of file extensions to consider as Texinfo logfiles.
371 The logfiles will be remove if `org-texinfo-remove-logfiles' is
372 non-nil."
373 :group 'org-export-texinfo
374 :type '(repeat (string :tag "Extension")))
376 (defcustom org-texinfo-remove-logfiles t
377 "Non-nil means remove the logfiles produced by compiling a Texinfo file.
378 By default, logfiles are files with these extensions: .aux, .toc,
379 .cp, .fn, .ky, .pg and .tp. To define the set of logfiles to remove,
380 set `org-texinfo-logfiles-extensions'."
381 :group 'org-export-latex
382 :type 'boolean)
384 ;;; Constants
386 (defconst org-texinfo-max-toc-depth 4
387 "Maximum depth for creation of detailed menu listings.
388 Beyond this depth, Texinfo will not recognize the nodes and will
389 cause errors. Left as a constant in case this value ever
390 changes.")
392 (defconst org-texinfo-supported-coding-systems
393 '("US-ASCII" "UTF-8" "ISO-8859-15" "ISO-8859-1" "ISO-8859-2" "koi8-r" "koi8-u")
394 "List of coding systems supported by Texinfo, as strings.
395 Specified coding system will be matched against these strings.
396 If two strings share the same prefix (e.g. \"ISO-8859-1\" and
397 \"ISO-8859-15\"), the most specific one has to be listed first.")
399 (defconst org-texinfo-inline-image-rules
400 (list (cons "file"
401 (regexp-opt '("eps" "pdf" "png" "jpg" "jpeg" "gif" "svg"))))
402 "Rules characterizing image files that can be inlined.")
405 ;;; Internal Functions
407 (defun org-texinfo--filter-section-blank-lines (headline _backend _info)
408 "Filter controlling number of blank lines after a section."
409 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" "\n\n" headline))
411 (defun org-texinfo--normalize-headlines (tree _backend info)
412 "Normalize headlines in TREE.
414 BACK-END is the symbol specifying back-end used for export. INFO
415 is a plist used as a communication channel.
417 Make sure every headline in TREE contains a section, since those
418 are required to install a menu. Also put exactly one blank line
419 at the end of each section.
421 Return new tree."
422 (org-element-map tree 'headline
423 (lambda (hl)
424 (org-element-put-property hl :post-blank 1)
425 (let ((contents (org-element-contents hl)))
426 (when contents
427 (let ((first (org-element-map contents '(headline section)
428 #'identity info t)))
429 (unless (eq (org-element-type first) 'section)
430 (apply #'org-element-set-contents
432 (cons `(section (:parent ,hl)) contents)))))))
433 info)
434 tree)
436 (defun org-texinfo--find-verb-separator (s)
437 "Return a character not used in string S.
438 This is used to choose a separator for constructs like \\verb."
439 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
440 (cl-loop for c across ll
441 when (not (string-match (regexp-quote (char-to-string c)) s))
442 return (char-to-string c))))
444 (defun org-texinfo--text-markup (text markup _info)
445 "Format TEXT depending on MARKUP text markup.
446 INFO is a plist used as a communication channel. See
447 `org-texinfo-text-markup-alist' for details."
448 (pcase (cdr (assq markup org-texinfo-text-markup-alist))
449 (`nil text) ;no markup: return raw text
450 (`code (format "@code{%s}" (org-texinfo--sanitize-content text)))
451 (`samp (format "@samp{%s}" (org-texinfo--sanitize-content text)))
452 (`verb
453 (let ((separator (org-texinfo--find-verb-separator text)))
454 (format "@verb{%s%s%s}" separator text separator)))
455 ;; Else use format string.
456 (fmt (format fmt text))))
458 (defun org-texinfo--get-node (datum info)
459 "Return node or anchor associated to DATUM.
460 DATUM is an element or object. INFO is a plist used as
461 a communication channel. The function guarantees the node or
462 anchor name is unique."
463 (let ((cache (plist-get info :texinfo-node-cache)))
464 (or (cdr (assq datum cache))
465 (let* ((salt 0)
466 (basename
467 (org-texinfo--sanitize-node
468 (if (eq (org-element-type datum) 'headline)
469 (org-texinfo--sanitize-title
470 (org-export-get-alt-title datum info) info)
471 (org-export-get-reference datum info))))
472 (name basename))
473 ;; Ensure NAME is unique and not reserved node name "Top".
474 (while (or (equal name "Top") (rassoc name cache))
475 (setq name (concat basename (format " %d" (cl-incf salt)))))
476 (plist-put info :texinfo-node-cache (cons (cons datum name) cache))
477 name))))
479 (defun org-texinfo--sanitize-node (title)
480 "Bend string TITLE to node line requirements.
481 Trim string and collapse multiple whitespace characters as they
482 are not significant. Replace leading left parenthesis, when
483 followed by a right parenthesis, with a square bracket. Remove
484 periods, commas and colons."
485 (org-trim
486 (replace-regexp-in-string
487 "[ \t]+" " "
488 (replace-regexp-in-string
489 "[:,.]" ""
490 (replace-regexp-in-string "\\`(\\(.*?)\\)" "[\\1" title)))))
492 (defun org-texinfo--sanitize-title (title info)
493 "Make TITLE suitable as a section name.
494 TITLE is a string or a secondary string. INFO is the current
495 export state, as a plist."
496 (org-export-data-with-backend
497 title
498 (org-export-create-backend
499 :parent 'texinfo
500 :transcoders '((footnote-reference . ignore)
501 (link . (lambda (object c i) c))
502 (radio-target . (lambda (object c i) c))
503 (target . ignore)))
504 info))
506 (defun org-texinfo--sanitize-content (text)
507 "Escape special characters in string TEXT.
508 Special characters are: @ { }"
509 (replace-regexp-in-string "[@{}]" "@\\&" text))
511 (defun org-texinfo--wrap-float (value info &optional type label caption short)
512 "Wrap string VALUE within a @float command.
513 INFO is the current export state, as a plist. TYPE is float
514 type, as a string. LABEL is the cross reference label for the
515 float, as a string. CAPTION and SHORT are, respectively, the
516 caption and shortcaption used for the float, as secondary
517 strings (e.g., returned by `org-export-get-caption')."
518 (let* ((backend
519 (org-export-create-backend
520 :parent 'texinfo
521 :transcoders '((link . (lambda (object c i) c))
522 (radio-target . (lambda (object c i) c))
523 (target . ignore))))
524 (short-backend
525 (org-export-create-backend
526 :parent 'texinfo
527 :transcoders '((footnote-reference . ignore)
528 (inline-src-block . ignore)
529 (link . (lambda (object c i) c))
530 (radio-target . (lambda (object c i) c))
531 (target . ignore)
532 (verbatim . ignore))))
533 (short-str
534 (if (and short caption)
535 (format "@shortcaption{%s}\n"
536 (org-export-data-with-backend short short-backend info))
537 ""))
538 (caption-str
539 (if (or short caption)
540 (format "@caption{%s}\n"
541 (org-export-data-with-backend
542 (or caption short)
543 (if (equal short-str "") short-backend backend)
544 info))
545 "")))
546 (format "@float %s%s\n%s\n%s%s@end float"
547 type (if label (concat "," label) "") value caption-str short-str)))
549 ;;; Template
551 (defun org-texinfo-template (contents info)
552 "Return complete document string after Texinfo conversion.
553 CONTENTS is the transcoded contents string. INFO is a plist
554 holding export options."
555 (let ((title (org-export-data (plist-get info :title) info))
556 ;; Copying data is the contents of the first headline in
557 ;; parse tree with a non-nil copying property.
558 (copying (org-element-map (plist-get info :parse-tree) 'headline
559 (lambda (hl)
560 (and (org-not-nil (org-element-property :COPYING hl))
561 (org-element-contents hl)))
562 info t)))
563 (concat
564 "\\input texinfo @c -*- texinfo -*-\n"
565 "@c %**start of header\n"
566 (let ((file (or (plist-get info :texinfo-filename)
567 (let ((f (plist-get info :output-file)))
568 (and f (concat (file-name-sans-extension f) ".info"))))))
569 (and file (format "@setfilename %s\n" file)))
570 (format "@settitle %s\n" title)
571 ;; Insert class-defined header.
572 (org-element-normalize-string
573 (let ((header (nth 1 (assoc (plist-get info :texinfo-class)
574 org-texinfo-classes)))
575 (coding
576 (catch 'coding-system
577 (let ((case-fold-search t)
578 (name (symbol-name (or org-texinfo-coding-system
579 buffer-file-coding-system))))
580 (dolist (system org-texinfo-supported-coding-systems "UTF-8")
581 (when (string-match-p (regexp-quote system) name)
582 (throw 'coding-system system))))))
583 (language (plist-get info :language))
584 (case-fold-search nil))
585 ;; Auto coding system.
586 (replace-regexp-in-string
587 "^@documentencoding \\(AUTO\\)$"
588 coding
589 (replace-regexp-in-string
590 "^@documentlanguage \\(AUTO\\)$" language header t nil 1) t nil 1)))
591 ;; Additional header options set by #+TEXINFO_HEADER.
592 (let ((texinfo-header (plist-get info :texinfo-header)))
593 (and texinfo-header (org-element-normalize-string texinfo-header)))
594 "@c %**end of header\n\n"
595 ;; Additional options set by #+TEXINFO_POST_HEADER.
596 (let ((texinfo-post-header (plist-get info :texinfo-post-header)))
597 (and texinfo-post-header
598 (org-element-normalize-string texinfo-post-header)))
599 ;; Copying.
600 (and copying
601 (format "@copying\n%s@end copying\n\n"
602 (org-element-normalize-string
603 (org-export-data copying info))))
604 ;; Info directory information. Only supply if both title and
605 ;; category are provided.
606 (let ((dircat (plist-get info :texinfo-dircat))
607 (dirtitle
608 (let ((title (plist-get info :texinfo-dirtitle)))
609 (and title
610 (string-match "^\\(?:\\* \\)?\\(.*?\\)\\(\\.\\)?$" title)
611 (format "* %s." (match-string 1 title))))))
612 (when (and dircat dirtitle)
613 (concat "@dircategory " dircat "\n"
614 "@direntry\n"
615 (let ((dirdesc
616 (let ((desc (plist-get info :texinfo-dirdesc)))
617 (cond ((not desc) nil)
618 ((string-suffix-p "." desc) desc)
619 (t (concat desc "."))))))
620 (if dirdesc (format "%-23s %s" dirtitle dirdesc) dirtitle))
621 "\n"
622 "@end direntry\n\n")))
623 ;; Title
624 "@finalout\n"
625 "@titlepage\n"
626 (when (plist-get info :with-title)
627 (concat
628 (format "@title %s\n" (or (plist-get info :texinfo-printed-title) title ""))
629 (let ((subtitle (plist-get info :subtitle)))
630 (when subtitle
631 (format "@subtitle %s\n"
632 (org-export-data subtitle info))))))
633 (when (plist-get info :with-author)
634 (concat
635 ;; Primary author.
636 (let ((author (org-string-nw-p
637 (org-export-data (plist-get info :author) info)))
638 (email (and (plist-get info :with-email)
639 (org-string-nw-p
640 (org-export-data (plist-get info :email) info)))))
641 (cond ((and author email)
642 (format "@author %s (@email{%s})\n" author email))
643 (author (format "@author %s\n" author))
644 (email (format "@author @email{%s}\n" email))))
645 ;; Other authors.
646 (let ((subauthor (plist-get info :subauthor)))
647 (and subauthor
648 (org-element-normalize-string
649 (replace-regexp-in-string "^" "@author " subauthor))))))
650 (and copying "@page\n@vskip 0pt plus 1filll\n@insertcopying\n")
651 "@end titlepage\n\n"
652 ;; Table of contents.
653 (and (plist-get info :with-toc) "@contents\n\n")
654 ;; Configure Top Node when not for Tex
655 "@ifnottex\n"
656 "@node Top\n"
657 (format "@top %s\n" title)
658 (and copying "@insertcopying\n")
659 "@end ifnottex\n\n"
660 ;; Menu.
661 (org-texinfo-make-menu (plist-get info :parse-tree) info 'master)
662 "\n"
663 ;; Document's body.
664 contents "\n"
665 ;; Creator.
666 (and (plist-get info :with-creator)
667 (concat (plist-get info :creator) "\n"))
668 ;; Document end.
669 "@bye")))
673 ;;; Transcode Functions
675 ;;;; Bold
677 (defun org-texinfo-bold (_bold contents info)
678 "Transcode BOLD from Org to Texinfo.
679 CONTENTS is the text with bold markup. INFO is a plist holding
680 contextual information."
681 (org-texinfo--text-markup contents 'bold info))
683 ;;;; Center Block
685 (defun org-texinfo-center-block (_center-block contents _info)
686 "Transcode a CENTER-BLOCK element from Org to Texinfo.
687 CONTENTS holds the contents of the block. INFO is a plist used
688 as a communication channel."
689 contents)
691 ;;;; Clock
693 (defun org-texinfo-clock (clock _contents info)
694 "Transcode a CLOCK element from Org to Texinfo.
695 CONTENTS is nil. INFO is a plist holding contextual
696 information."
697 (concat
698 "@noindent"
699 (format "@strong{%s} " org-clock-string)
700 (format (plist-get info :texinfo-inactive-timestamp-format)
701 (concat (org-timestamp-translate (org-element-property :value clock))
702 (let ((time (org-element-property :duration clock)))
703 (and time (format " (%s)" time)))))
704 "@*"))
706 ;;;; Code
708 (defun org-texinfo-code (code _contents info)
709 "Transcode a CODE object from Org to Texinfo.
710 CONTENTS is nil. INFO is a plist used as a communication
711 channel."
712 (org-texinfo--text-markup (org-element-property :value code) 'code info))
714 ;;;; Drawer
716 (defun org-texinfo-drawer (drawer contents info)
717 "Transcode a DRAWER element from Org to Texinfo.
718 CONTENTS holds the contents of the block. INFO is a plist
719 holding contextual information."
720 (let* ((name (org-element-property :drawer-name drawer))
721 (output (funcall (plist-get info :texinfo-format-drawer-function)
722 name contents)))
723 output))
725 ;;;; Dynamic Block
727 (defun org-texinfo-dynamic-block (_dynamic-block contents _info)
728 "Transcode a DYNAMIC-BLOCK element from Org to Texinfo.
729 CONTENTS holds the contents of the block. INFO is a plist
730 holding contextual information."
731 contents)
733 ;;;; Entity
735 (defun org-texinfo-entity (entity _contents _info)
736 "Transcode an ENTITY object from Org to Texinfo.
737 CONTENTS are the definition itself. INFO is a plist holding
738 contextual information."
739 (let ((ent (org-element-property :latex entity)))
740 (if (org-element-property :latex-math-p entity) (format "@math{%s}" ent) ent)))
742 ;;;; Example Block
744 (defun org-texinfo-example-block (example-block _contents info)
745 "Transcode an EXAMPLE-BLOCK element from Org to Texinfo.
746 CONTENTS is nil. INFO is a plist holding contextual
747 information."
748 (format "@verbatim\n%s@end verbatim"
749 (org-export-format-code-default example-block info)))
751 ;;; Export Block
753 (defun org-texinfo-export-block (export-block _contents _info)
754 "Transcode a EXPORT-BLOCK element from Org to Texinfo.
755 CONTENTS is nil. INFO is a plist holding contextual information."
756 (when (string= (org-element-property :type export-block) "TEXINFO")
757 (org-remove-indentation (org-element-property :value export-block))))
759 ;;; Export Snippet
761 (defun org-texinfo-export-snippet (export-snippet _contents _info)
762 "Transcode a EXPORT-SNIPPET object from Org to Texinfo.
763 CONTENTS is nil. INFO is a plist holding contextual information."
764 (when (eq (org-export-snippet-backend export-snippet) 'texinfo)
765 (org-element-property :value export-snippet)))
767 ;;;; Fixed Width
769 (defun org-texinfo-fixed-width (fixed-width _contents _info)
770 "Transcode a FIXED-WIDTH element from Org to Texinfo.
771 CONTENTS is nil. INFO is a plist holding contextual information."
772 (format "@example\n%s@end example"
773 (org-remove-indentation
774 (org-texinfo--sanitize-content
775 (org-element-property :value fixed-width)))))
777 ;;;; Footnote Reference
779 (defun org-texinfo-footnote-reference (footnote _contents info)
780 "Create a footnote reference for FOOTNOTE.
782 FOOTNOTE is the footnote to define. CONTENTS is nil. INFO is a
783 plist holding contextual information."
784 (let ((def (org-export-get-footnote-definition footnote info)))
785 (format "@footnote{%s}"
786 (org-trim (org-export-data def info)))))
788 ;;;; Headline
790 (defun org-texinfo-headline (headline contents info)
791 "Transcode a HEADLINE element from Org to Texinfo.
792 CONTENTS holds the contents of the headline. INFO is a plist
793 holding contextual information."
794 (let* ((class (plist-get info :texinfo-class))
795 (level (org-export-get-relative-level headline info))
796 (numberedp (org-export-numbered-headline-p headline info))
797 (class-sectioning (assoc class (plist-get info :texinfo-classes)))
798 ;; Find the index type, if any.
799 (index (org-element-property :INDEX headline))
800 ;; Create node info, to insert it before section formatting.
801 ;; Use custom menu title if present.
802 (node (format "@node %s\n" (org-texinfo--get-node headline info)))
803 ;; Section formatting will set two placeholders: one for the
804 ;; title and the other for the contents.
805 (section-fmt
806 (if (org-not-nil (org-element-property :APPENDIX headline))
807 "@appendix %s\n%s"
808 (let ((sec (if (and (symbolp (nth 2 class-sectioning))
809 (fboundp (nth 2 class-sectioning)))
810 (funcall (nth 2 class-sectioning) level numberedp)
811 (nth (1+ level) class-sectioning))))
812 (cond
813 ;; No section available for that LEVEL.
814 ((not sec) nil)
815 ;; Section format directly returned by a function.
816 ((stringp sec) sec)
817 ;; (numbered-section . unnumbered-section)
818 ((not (consp (cdr sec)))
819 (concat (if (or index (not numberedp)) (cdr sec) (car sec))
820 "\n%s"))))))
821 (todo
822 (and (plist-get info :with-todo-keywords)
823 (let ((todo (org-element-property :todo-keyword headline)))
824 (and todo (org-export-data todo info)))))
825 (todo-type (and todo (org-element-property :todo-type headline)))
826 (tags (and (plist-get info :with-tags)
827 (org-export-get-tags headline info)))
828 (priority (and (plist-get info :with-priority)
829 (org-element-property :priority headline)))
830 (text (org-texinfo--sanitize-title
831 (org-element-property :title headline) info))
832 (full-text (funcall (plist-get info :texinfo-format-headline-function)
833 todo todo-type priority text tags))
834 (contents (if (org-string-nw-p contents) (concat "\n" contents) "")))
835 (cond
836 ;; Case 1: This is a footnote section: ignore it.
837 ((org-element-property :footnote-section-p headline) nil)
838 ;; Case 2: This is the `copying' section: ignore it
839 ;; This is used elsewhere.
840 ((org-not-nil (org-element-property :COPYING headline)) nil)
841 ;; Case 3: An index. If it matches one of the known indexes,
842 ;; print it as such following the contents, otherwise
843 ;; print the contents and leave the index up to the user.
844 (index
845 (concat node
846 (format
847 section-fmt
848 full-text
849 (concat contents
850 (and (member index '("cp" "fn" "ky" "pg" "tp" "vr"))
851 (concat "\n@printindex " index))))))
852 ;; Case 4: This is a deep sub-tree: export it as a list item.
853 ;; Also export as items headlines for which no section
854 ;; format has been found.
855 ((or (not section-fmt) (org-export-low-level-p headline info))
856 ;; Build the real contents of the sub-tree.
857 (concat (and (org-export-first-sibling-p headline info)
858 (format "@%s\n" (if numberedp 'enumerate 'itemize)))
859 "@item\n" full-text "\n"
860 contents
861 (if (org-export-last-sibling-p headline info)
862 (format "@end %s" (if numberedp 'enumerate 'itemize))
863 "\n")))
864 ;; Case 5: Standard headline. Export it as a section.
865 (t (concat node (format section-fmt full-text contents))))))
867 (defun org-texinfo-format-headline-default-function
868 (todo _todo-type priority text tags)
869 "Default format function for a headline.
870 See `org-texinfo-format-headline-function' for details."
871 (concat (when todo (format "@strong{%s} " todo))
872 (when priority (format "@emph{#%s} " priority))
873 text
874 (when tags (format " :%s:" (mapconcat 'identity tags ":")))))
876 ;;;; Inline Src Block
878 (defun org-texinfo-inline-src-block (inline-src-block _contents _info)
879 "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
880 CONTENTS holds the contents of the item. INFO is a plist holding
881 contextual information."
882 (format "@code{%s}"
883 (org-texinfo--sanitize-content
884 (org-element-property :value inline-src-block))))
886 ;;;; Inlinetask
888 (defun org-texinfo-inlinetask (inlinetask contents info)
889 "Transcode an INLINETASK element from Org to Texinfo.
890 CONTENTS holds the contents of the block. INFO is a plist
891 holding contextual information."
892 (let ((title (org-export-data (org-element-property :title inlinetask) info))
893 (todo (and (plist-get info :with-todo-keywords)
894 (let ((todo (org-element-property :todo-keyword inlinetask)))
895 (and todo (org-export-data todo info)))))
896 (todo-type (org-element-property :todo-type inlinetask))
897 (tags (and (plist-get info :with-tags)
898 (org-export-get-tags inlinetask info)))
899 (priority (and (plist-get info :with-priority)
900 (org-element-property :priority inlinetask))))
901 (funcall (plist-get info :texinfo-format-inlinetask-function)
902 todo todo-type priority title tags contents)))
904 (defun org-texinfo-format-inlinetask-default-function
905 (todo _todo-type priority title tags contents)
906 "Default format function for a inlinetasks.
907 See `org-texinfo-format-inlinetask-function' for details."
908 (let ((full-title
909 (concat (when todo (format "@strong{%s} " todo))
910 (when priority (format "#%c " priority))
911 title
912 (when tags (format ":%s:" (mapconcat #'identity tags ":"))))))
913 (format "@center %s\n\n%s\n" full-title contents)))
915 ;;;; Italic
917 (defun org-texinfo-italic (_italic contents info)
918 "Transcode ITALIC from Org to Texinfo.
919 CONTENTS is the text with italic markup. INFO is a plist holding
920 contextual information."
921 (org-texinfo--text-markup contents 'italic info))
923 ;;;; Item
925 (defun org-texinfo-item (item contents info)
926 "Transcode an ITEM element from Org to Texinfo.
927 CONTENTS holds the contents of the item. INFO is a plist holding
928 contextual information."
929 (let* ((tag (org-element-property :tag item))
930 (split (org-string-nw-p
931 (org-export-read-attribute :attr_texinfo
932 (org-element-property :parent item)
933 :sep)))
934 (items (and tag
935 (let ((tag (org-export-data tag info)))
936 (if split (split-string tag split t "[ \t\n]+")
937 (list tag))))))
938 (format "%s\n%s"
939 (pcase items
940 (`nil "@item")
941 (`(,item) (concat "@item " item))
942 (`(,item . ,items)
943 (concat "@item " item "\n"
944 (mapconcat (lambda (i) (concat "@itemx " i))
945 items
946 "\n"))))
947 (or contents ""))))
949 ;;;; Keyword
951 (defun org-texinfo-keyword (keyword _contents info)
952 "Transcode a KEYWORD element from Org to Texinfo.
953 CONTENTS is nil. INFO is a plist holding contextual information."
954 (let ((key (org-element-property :key keyword))
955 (value (org-element-property :value keyword)))
956 (cond
957 ((string= key "TEXINFO") value)
958 ((string= key "CINDEX") (format "@cindex %s" value))
959 ((string= key "FINDEX") (format "@findex %s" value))
960 ((string= key "KINDEX") (format "@kindex %s" value))
961 ((string= key "PINDEX") (format "@pindex %s" value))
962 ((string= key "TINDEX") (format "@tindex %s" value))
963 ((string= key "VINDEX") (format "@vindex %s" value))
964 ((string= key "TOC")
965 (cond ((string-match-p "\\<tables\\>" value)
966 (concat "@listoffloats "
967 (org-export-translate "Table" :utf-8 info)))
968 ((string-match-p "\\<listings\\>" value)
969 (concat "@listoffloats "
970 (org-export-translate "Listing" :utf-8 info))))))))
972 ;;;; Line Break
974 (defun org-texinfo-line-break (_line-break _contents _info)
975 "Transcode a LINE-BREAK object from Org to Texinfo.
976 CONTENTS is nil. INFO is a plist holding contextual information."
977 "@*\n")
979 ;;;; Link
981 (defun org-texinfo--@ref (datum description info)
982 "Return @ref command for element or object DATUM.
983 DESCRIPTION is the name of the section to print, as a string."
984 (let ((node-name (org-texinfo--get-node datum info))
985 ;; Sanitize DESCRIPTION for cross-reference use. In
986 ;; particular, remove colons as they seem to cause (even
987 ;; within @asis{...} to the Texinfo reader.
988 (title (replace-regexp-in-string
989 "[ \t]*:+" ""
990 (replace-regexp-in-string "," "@comma{}" description))))
991 (if (equal title node-name)
992 (format "@ref{%s}" node-name)
993 (format "@ref{%s, , %s}" node-name title))))
995 (defun org-texinfo-link (link desc info)
996 "Transcode a LINK object from Org to Texinfo.
997 DESC is the description part of the link, or the empty string.
998 INFO is a plist holding contextual information. See
999 `org-export-data'."
1000 (let* ((type (org-element-property :type link))
1001 (raw-path (org-element-property :path link))
1002 ;; Ensure DESC really exists, or set it to nil.
1003 (desc (and (not (string= desc "")) desc))
1004 (path (cond
1005 ((member type '("http" "https" "ftp"))
1006 (concat type ":" raw-path))
1007 ((string= type "file") (org-export-file-uri raw-path))
1008 (t raw-path))))
1009 (cond
1010 ((org-export-custom-protocol-maybe link desc 'texinfo))
1011 ((org-export-inline-image-p link org-texinfo-inline-image-rules)
1012 (org-texinfo--inline-image link info))
1013 ((equal type "radio")
1014 (let ((destination (org-export-resolve-radio-link link info)))
1015 (if (not destination) desc
1016 (org-texinfo--@ref destination desc info))))
1017 ((member type '("custom-id" "id" "fuzzy"))
1018 (let ((destination
1019 (if (equal type "fuzzy")
1020 (org-export-resolve-fuzzy-link link info)
1021 (org-export-resolve-id-link link info))))
1022 (pcase (org-element-type destination)
1023 (`nil
1024 (format org-texinfo-link-with-unknown-path-format
1025 (org-texinfo--sanitize-content path)))
1026 ;; Id link points to an external file.
1027 (`plain-text
1028 (if desc (format "@uref{file://%s,%s}" destination desc)
1029 (format "@uref{file://%s}" destination)))
1030 ((or `headline
1031 ;; Targets within headlines cannot be turned into
1032 ;; @anchor{}, so we refer to the headline parent
1033 ;; directly.
1034 (and `target
1035 (guard (eq 'headline
1036 (org-element-type
1037 (org-element-property :parent destination))))))
1038 (let ((headline (org-element-lineage destination '(headline) t)))
1039 (org-texinfo--@ref
1040 headline
1041 (or desc (org-texinfo--sanitize-title
1042 (org-element-property :title headline) info))
1043 info)))
1045 (org-texinfo--@ref
1046 destination
1047 (or desc
1048 (pcase (org-export-get-ordinal destination info)
1049 ((and (pred integerp) n) (number-to-string n))
1050 ((and (pred consp) n) (mapconcat #'number-to-string n "."))
1051 (_ "???"))) ;cannot guess the description
1052 info)))))
1053 ((string= type "mailto")
1054 (format "@email{%s}"
1055 (concat (org-texinfo--sanitize-content path)
1056 (and desc (concat ", " desc)))))
1057 ;; External link with a description part.
1058 ((and path desc) (format "@uref{%s, %s}" path desc))
1059 ;; External link without a description part.
1060 (path (format "@uref{%s}" path))
1061 ;; No path, only description. Try to do something useful.
1063 (format (plist-get info :texinfo-link-with-unknown-path-format) desc)))))
1065 (defun org-texinfo--inline-image (link info)
1066 "Return Texinfo code for an inline image.
1067 LINK is the link pointing to the inline image. INFO is the
1068 current state of the export, as a plist."
1069 (let* ((parent (org-export-get-parent-element link))
1070 (label (and (org-element-property :name parent)
1071 (org-texinfo--get-node parent info)))
1072 (caption (org-export-get-caption parent))
1073 (shortcaption (org-export-get-caption parent t))
1074 (path (org-element-property :path link))
1075 (filename
1076 (file-name-sans-extension
1077 (if (file-name-absolute-p path) (expand-file-name path) path)))
1078 (extension (file-name-extension path))
1079 (attributes (org-export-read-attribute :attr_texinfo parent))
1080 (height (or (plist-get attributes :height) ""))
1081 (width (or (plist-get attributes :width) ""))
1082 (alt (or (plist-get attributes :alt) ""))
1083 (image (format "@image{%s,%s,%s,%s,%s}"
1084 filename width height alt extension)))
1085 (cond ((or caption shortcaption)
1086 (org-texinfo--wrap-float image
1087 info
1088 (org-export-translate "Figure" :utf-8 info)
1089 label
1090 caption
1091 shortcaption))
1092 (label (concat "@anchor{" label "}\n" image))
1093 (t image))))
1096 ;;;; Menu
1098 (defun org-texinfo-make-menu (scope info &optional master)
1099 "Create the menu for inclusion in the Texinfo document.
1101 SCOPE is a headline or a full parse tree. INFO is the
1102 communication channel, as a plist.
1104 When optional argument MASTER is non-nil, generate a master menu,
1105 including detailed node listing."
1106 (let ((menu (org-texinfo--build-menu scope info)))
1107 (when (org-string-nw-p menu)
1108 (org-element-normalize-string
1109 (format
1110 "@menu\n%s@end menu"
1111 (concat menu
1112 (when master
1113 (let ((detailmenu
1114 (org-texinfo--build-menu
1115 scope info
1116 (let ((toc-depth (plist-get info :with-toc)))
1117 (if (wholenump toc-depth) toc-depth
1118 org-texinfo-max-toc-depth)))))
1119 (when (org-string-nw-p detailmenu)
1120 (concat "\n@detailmenu\n"
1121 "--- The Detailed Node Listing ---\n\n"
1122 detailmenu
1123 "@end detailmenu\n"))))))))))
1125 (defun org-texinfo--build-menu (scope info &optional level)
1126 "Build menu for entries within SCOPE.
1127 SCOPE is a headline or a full parse tree. INFO is a plist
1128 containing contextual information. When optional argument LEVEL
1129 is an integer, build the menu recursively, down to this depth."
1130 (cond
1131 ((not level)
1132 (org-texinfo--format-entries (org-texinfo--menu-entries scope info) info))
1133 ((zerop level) nil)
1135 (org-element-normalize-string
1136 (mapconcat
1137 (lambda (h)
1138 (let ((entries (org-texinfo--menu-entries h info)))
1139 (when entries
1140 (concat
1141 (format "%s\n\n%s\n"
1142 (org-export-data (org-export-get-alt-title h info) info)
1143 (org-texinfo--format-entries entries info))
1144 (org-texinfo--build-menu h info (1- level))))))
1145 (org-texinfo--menu-entries scope info) "\n")))))
1147 (defun org-texinfo--format-entries (entries info)
1148 "Format all direct menu entries in SCOPE, as a string.
1149 SCOPE is either a headline or a full Org document. INFO is
1150 a plist containing contextual information."
1151 (org-element-normalize-string
1152 (mapconcat
1153 (lambda (h)
1154 (let* ((title
1155 ;; Colons are used as a separator between title and node
1156 ;; name. Remove them.
1157 (replace-regexp-in-string
1158 "[ \t]+:+" ""
1159 (org-texinfo--sanitize-title
1160 (org-export-get-alt-title h info) info)))
1161 (node (org-texinfo--get-node h info))
1162 (entry (concat "* " title ":"
1163 (if (string= title node) ":"
1164 (concat " " node ". "))))
1165 (desc (org-element-property :DESCRIPTION h)))
1166 (if (not desc) entry
1167 (format (format "%%-%ds %%s" org-texinfo-node-description-column)
1168 entry desc))))
1169 entries "\n")))
1171 (defun org-texinfo--menu-entries (scope info)
1172 "List direct children in SCOPE needing a menu entry.
1173 SCOPE is a headline or a full parse tree. INFO is a plist
1174 holding contextual information."
1175 (let* ((cache (or (plist-get info :texinfo-entries-cache)
1176 (plist-get (plist-put info :texinfo-entries-cache
1177 (make-hash-table :test #'eq))
1178 :texinfo-entries-cache)))
1179 (cached-entries (gethash scope cache 'no-cache)))
1180 (if (not (eq cached-entries 'no-cache)) cached-entries
1181 (puthash scope
1182 (org-element-map (org-element-contents scope) 'headline
1183 (lambda (h)
1184 (and (not (org-not-nil (org-element-property :COPYING h)))
1185 (not (org-element-property :footnote-section-p h))
1186 (not (org-export-low-level-p h info))
1188 info nil 'headline)
1189 cache))))
1191 ;;;; Node Property
1193 (defun org-texinfo-node-property (node-property _contents _info)
1194 "Transcode a NODE-PROPERTY element from Org to Texinfo.
1195 CONTENTS is nil. INFO is a plist holding contextual
1196 information."
1197 (format "%s:%s"
1198 (org-element-property :key node-property)
1199 (let ((value (org-element-property :value node-property)))
1200 (if value (concat " " value) ""))))
1202 ;;;; Paragraph
1204 (defun org-texinfo-paragraph (_paragraph contents _info)
1205 "Transcode a PARAGRAPH element from Org to Texinfo.
1206 CONTENTS is the contents of the paragraph, as a string. INFO is
1207 the plist used as a communication channel."
1208 contents)
1210 ;;;; Plain List
1212 (defun org-texinfo-plain-list (plain-list contents info)
1213 "Transcode a PLAIN-LIST element from Org to Texinfo.
1214 CONTENTS is the contents of the list. INFO is a plist holding
1215 contextual information."
1216 (let* ((attr (org-export-read-attribute :attr_texinfo plain-list))
1217 (indic (let ((i (or (plist-get attr :indic)
1218 (plist-get info :texinfo-def-table-markup))))
1219 ;; Allow indicating commands with missing @ sign.
1220 (if (string-prefix-p "@" i) i (concat "@" i))))
1221 (table-type (plist-get attr :table-type))
1222 (type (org-element-property :type plain-list))
1223 (list-type (cond
1224 ((eq type 'ordered) "enumerate")
1225 ((eq type 'unordered) "itemize")
1226 ((member table-type '("ftable" "vtable")) table-type)
1227 (t "table"))))
1228 (format "@%s\n%s@end %s"
1229 (if (eq type 'descriptive) (concat list-type " " indic) list-type)
1230 contents
1231 list-type)))
1233 ;;;; Plain Text
1235 (defun org-texinfo-plain-text (text info)
1236 "Transcode a TEXT string from Org to Texinfo.
1237 TEXT is the string to transcode. INFO is a plist holding
1238 contextual information."
1239 ;; First protect @, { and }.
1240 (let ((output (org-texinfo--sanitize-content text)))
1241 ;; Activate smart quotes. Be sure to provide original TEXT string
1242 ;; since OUTPUT may have been modified.
1243 (when (plist-get info :with-smart-quotes)
1244 (setq output
1245 (org-export-activate-smart-quotes output :texinfo info text)))
1246 ;; LaTeX into @LaTeX{} and TeX into @TeX{}
1247 (let ((case-fold-search nil)
1248 (start 0))
1249 (while (string-match "\\(\\(?:La\\)?TeX\\)" output start)
1250 (setq output (replace-match
1251 (format "@%s{}" (match-string 1 output)) nil t output)
1252 start (match-end 0))))
1253 ;; Convert special strings.
1254 (when (plist-get info :with-special-strings)
1255 (while (string-match (regexp-quote "...") output)
1256 (setq output (replace-match "@dots{}" nil t output))))
1257 ;; Handle break preservation if required.
1258 (when (plist-get info :preserve-breaks)
1259 (setq output (replace-regexp-in-string
1260 "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" output)))
1261 ;; Return value.
1262 output))
1264 ;;;; Planning
1266 (defun org-texinfo-planning (planning _contents info)
1267 "Transcode a PLANNING element from Org to Texinfo.
1268 CONTENTS is nil. INFO is a plist holding contextual
1269 information."
1270 (concat
1271 "@noindent"
1272 (mapconcat
1273 'identity
1274 (delq nil
1275 (list
1276 (let ((closed (org-element-property :closed planning)))
1277 (when closed
1278 (concat
1279 (format "@strong{%s} " org-closed-string)
1280 (format (plist-get info :texinfo-inactive-timestamp-format)
1281 (org-timestamp-translate closed)))))
1282 (let ((deadline (org-element-property :deadline planning)))
1283 (when deadline
1284 (concat
1285 (format "@strong{%s} " org-deadline-string)
1286 (format (plist-get info :texinfo-active-timestamp-format)
1287 (org-timestamp-translate deadline)))))
1288 (let ((scheduled (org-element-property :scheduled planning)))
1289 (when scheduled
1290 (concat
1291 (format "@strong{%s} " org-scheduled-string)
1292 (format (plist-get info :texinfo-active-timestamp-format)
1293 (org-timestamp-translate scheduled)))))))
1294 " ")
1295 "@*"))
1297 ;;;; Property Drawer
1299 (defun org-texinfo-property-drawer (_property-drawer contents _info)
1300 "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
1301 CONTENTS holds the contents of the drawer. INFO is a plist
1302 holding contextual information."
1303 (and (org-string-nw-p contents)
1304 (format "@verbatim\n%s@end verbatim" contents)))
1306 ;;;; Quote Block
1308 (defun org-texinfo-quote-block (quote-block contents _info)
1309 "Transcode a QUOTE-BLOCK element from Org to Texinfo.
1310 CONTENTS holds the contents of the block. INFO is a plist
1311 holding contextual information."
1312 (let* ((title (org-element-property :name quote-block))
1313 (start-quote (concat "@quotation"
1314 (if title
1315 (format " %s" title)))))
1316 (format "%s\n%s@end quotation" start-quote contents)))
1318 ;;;; Radio Target
1320 (defun org-texinfo-radio-target (radio-target text info)
1321 "Transcode a RADIO-TARGET object from Org to Texinfo.
1322 TEXT is the text of the target. INFO is a plist holding
1323 contextual information."
1324 (format "@anchor{%s}%s"
1325 (org-texinfo--get-node radio-target info)
1326 text))
1328 ;;;; Section
1330 (defun org-texinfo-section (section contents info)
1331 "Transcode a SECTION element from Org to Texinfo.
1332 CONTENTS holds the contents of the section. INFO is a plist
1333 holding contextual information."
1334 (org-trim
1335 (concat contents
1336 "\n"
1337 (let ((parent (org-export-get-parent-headline section)))
1338 (and parent (org-texinfo-make-menu parent info))))))
1340 ;;;; Special Block
1342 (defun org-texinfo-special-block (special-block contents _info)
1343 "Transcode a SPECIAL-BLOCK element from Org to Texinfo.
1344 CONTENTS holds the contents of the block. INFO is a plist used
1345 as a communication channel."
1346 (let ((opt (org-export-read-attribute :attr_texinfo special-block :options))
1347 (type (org-element-property :type special-block)))
1348 (format "@%s%s\n%s@end %s"
1349 type
1350 (if opt (concat " " opt) opt)
1351 (or contents "")
1352 type)))
1354 ;;;; Src Block
1356 (defun org-texinfo-src-block (src-block _contents info)
1357 "Transcode a SRC-BLOCK element from Org to Texinfo.
1358 CONTENTS holds the contents of the item. INFO is a plist holding
1359 contextual information."
1360 (let* ((lisp (string-match-p "lisp"
1361 (org-element-property :language src-block)))
1362 (code (org-texinfo--sanitize-content
1363 (org-export-format-code-default src-block info)))
1364 (value (format
1365 (if lisp "@lisp\n%s@end lisp" "@example\n%s@end example")
1366 code))
1367 (caption (org-export-get-caption src-block))
1368 (shortcaption (org-export-get-caption src-block t)))
1369 (if (not (or caption shortcaption)) value
1370 (org-texinfo--wrap-float value
1371 info
1372 (org-export-translate "Listing" :utf-8 info)
1373 (org-texinfo--get-node src-block info)
1374 caption
1375 shortcaption))))
1377 ;;;; Statistics Cookie
1379 (defun org-texinfo-statistics-cookie (statistics-cookie _contents _info)
1380 "Transcode a STATISTICS-COOKIE object from Org to Texinfo.
1381 CONTENTS is nil. INFO is a plist holding contextual information."
1382 (org-element-property :value statistics-cookie))
1385 ;;;; Strike-through
1387 (defun org-texinfo-strike-through (_strike-through contents info)
1388 "Transcode STRIKE-THROUGH from Org to Texinfo.
1389 CONTENTS is the text with strike-through markup. INFO is a plist
1390 holding contextual information."
1391 (org-texinfo--text-markup contents 'strike-through info))
1393 ;;;; Subscript
1395 (defun org-texinfo-subscript (_subscript contents _info)
1396 "Transcode a SUBSCRIPT object from Org to Texinfo.
1397 CONTENTS is the contents of the object. INFO is a plist holding
1398 contextual information."
1399 (format "@math{_%s}" contents))
1401 ;;;; Superscript
1403 (defun org-texinfo-superscript (_superscript contents _info)
1404 "Transcode a SUPERSCRIPT object from Org to Texinfo.
1405 CONTENTS is the contents of the object. INFO is a plist holding
1406 contextual information."
1407 (format "@math{^%s}" contents))
1409 ;;;; Table
1411 (defun org-texinfo-table (table contents info)
1412 "Transcode a TABLE element from Org to Texinfo.
1413 CONTENTS is the contents of the table. INFO is a plist holding
1414 contextual information."
1415 (if (eq (org-element-property :type table) 'table.el)
1416 (format "@verbatim\n%s@end verbatim"
1417 (org-element-normalize-string
1418 (org-element-property :value table)))
1419 (let* ((col-width (org-export-read-attribute :attr_texinfo table :columns))
1420 (columns
1421 (if col-width (format "@columnfractions %s" col-width)
1422 (org-texinfo-table-column-widths table info)))
1423 (caption (org-export-get-caption table))
1424 (shortcaption (org-export-get-caption table t))
1425 (table-str (format "@multitable %s\n%s@end multitable"
1426 columns
1427 contents)))
1428 (if (not (or caption shortcaption)) table-str
1429 (org-texinfo--wrap-float table-str
1430 info
1431 (org-export-translate "Table" :utf-8 info)
1432 (org-texinfo--get-node table info)
1433 caption
1434 shortcaption)))))
1436 (defun org-texinfo-table-column-widths (table info)
1437 "Determine the largest table cell in each column to process alignment.
1438 TABLE is the table element to transcode. INFO is a plist used as
1439 a communication channel."
1440 (let ((widths (make-vector (cdr (org-export-table-dimensions table info)) 0)))
1441 (org-element-map table 'table-row
1442 (lambda (row)
1443 (let ((idx 0))
1444 (org-element-map row 'table-cell
1445 (lambda (cell)
1446 ;; Length of the cell in the original buffer is only an
1447 ;; approximation of the length of the cell in the
1448 ;; output. It can sometimes fail (e.g. it considers
1449 ;; "/a/" being larger than "ab").
1450 (let ((w (- (org-element-property :contents-end cell)
1451 (org-element-property :contents-begin cell))))
1452 (aset widths idx (max w (aref widths idx))))
1453 (cl-incf idx))
1454 info)))
1455 info)
1456 (format "{%s}" (mapconcat (lambda (w) (make-string w ?a)) widths "} {"))))
1458 ;;;; Table Cell
1460 (defun org-texinfo-table-cell (table-cell contents info)
1461 "Transcode a TABLE-CELL element from Org to Texinfo.
1462 CONTENTS is the cell contents. INFO is a plist used as
1463 a communication channel."
1464 (concat
1465 (let ((scientific-notation
1466 (plist-get info :texinfo-table-scientific-notation)))
1467 (if (and contents
1468 scientific-notation
1469 (string-match orgtbl-exp-regexp contents))
1470 ;; Use appropriate format string for scientific notation.
1471 (format scientific-notation
1472 (match-string 1 contents)
1473 (match-string 2 contents))
1474 contents))
1475 (when (org-export-get-next-element table-cell info) "\n@tab ")))
1477 ;;;; Table Row
1479 (defun org-texinfo-table-row (table-row contents info)
1480 "Transcode a TABLE-ROW element from Org to Texinfo.
1481 CONTENTS is the contents of the row. INFO is a plist used as
1482 a communication channel."
1483 ;; Rules are ignored since table separators are deduced from
1484 ;; borders of the current row.
1485 (when (eq (org-element-property :type table-row) 'standard)
1486 (let ((rowgroup-tag
1487 (if (and (= 1 (org-export-table-row-group table-row info))
1488 (org-export-table-has-header-p
1489 (org-export-get-parent-table table-row) info))
1490 "@headitem "
1491 "@item ")))
1492 (concat rowgroup-tag contents "\n"))))
1494 ;;;; Target
1496 (defun org-texinfo-target (target _contents info)
1497 "Transcode a TARGET object from Org to Texinfo.
1498 CONTENTS is nil. INFO is a plist holding contextual
1499 information."
1500 (format "@anchor{%s}" (org-texinfo--get-node target info)))
1502 ;;;; Timestamp
1504 (defun org-texinfo-timestamp (timestamp _contents info)
1505 "Transcode a TIMESTAMP object from Org to Texinfo.
1506 CONTENTS is nil. INFO is a plist holding contextual
1507 information."
1508 (let ((value (org-texinfo-plain-text
1509 (org-timestamp-translate timestamp) info)))
1510 (pcase (org-element-property :type timestamp)
1511 ((or `active `active-range)
1512 (format (plist-get info :texinfo-active-timestamp-format) value))
1513 ((or `inactive `inactive-range)
1514 (format (plist-get info :texinfo-inactive-timestamp-format) value))
1515 (_ (format (plist-get info :texinfo-diary-timestamp-format) value)))))
1517 ;;;; Underline
1519 (defun org-texinfo-underline (_underline contents info)
1520 "Transcode UNDERLINE from Org to Texinfo.
1521 CONTENTS is the text with underline markup. INFO is a plist
1522 holding contextual information."
1523 (org-texinfo--text-markup contents 'underline info))
1525 ;;;; Verbatim
1527 (defun org-texinfo-verbatim (verbatim _contents info)
1528 "Transcode a VERBATIM object from Org to Texinfo.
1529 CONTENTS is nil. INFO is a plist used as a communication
1530 channel."
1531 (org-texinfo--text-markup
1532 (org-element-property :value verbatim) 'verbatim info))
1534 ;;;; Verse Block
1536 (defun org-texinfo-verse-block (_verse-block contents _info)
1537 "Transcode a VERSE-BLOCK element from Org to Texinfo.
1538 CONTENTS is verse block contents. INFO is a plist holding
1539 contextual information."
1540 (format "@display\n%s@end display" contents))
1543 ;;; Interactive functions
1545 (defun org-texinfo-export-to-texinfo
1546 (&optional async subtreep visible-only body-only ext-plist)
1547 "Export current buffer to a Texinfo file.
1549 If narrowing is active in the current buffer, only export its
1550 narrowed part.
1552 If a region is active, export that region.
1554 A non-nil optional argument ASYNC means the process should happen
1555 asynchronously. The resulting file should be accessible through
1556 the `org-export-stack' interface.
1558 When optional argument SUBTREEP is non-nil, export the sub-tree
1559 at point, extracting information from the headline properties
1560 first.
1562 When optional argument VISIBLE-ONLY is non-nil, don't export
1563 contents of hidden elements.
1565 When optional argument BODY-ONLY is non-nil, only write code
1566 between \"\\begin{document}\" and \"\\end{document}\".
1568 EXT-PLIST, when provided, is a property list with external
1569 parameters overriding Org default settings, but still inferior to
1570 file-local settings.
1572 Return output file's name."
1573 (interactive)
1574 (let ((outfile (org-export-output-file-name ".texi" subtreep))
1575 (org-export-coding-system org-texinfo-coding-system))
1576 (org-export-to-file 'texinfo outfile
1577 async subtreep visible-only body-only ext-plist)))
1579 (defun org-texinfo-export-to-info
1580 (&optional async subtreep visible-only body-only ext-plist)
1581 "Export current buffer to Texinfo then process through to INFO.
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 When optional argument PUB-DIR is set, use it as the publishing
1607 directory.
1609 Return INFO file's name."
1610 (interactive)
1611 (let ((outfile (org-export-output-file-name ".texi" subtreep))
1612 (org-export-coding-system org-texinfo-coding-system))
1613 (org-export-to-file 'texinfo outfile
1614 async subtreep visible-only body-only ext-plist
1615 (lambda (file) (org-texinfo-compile file)))))
1617 ;;;###autoload
1618 (defun org-texinfo-publish-to-texinfo (plist filename pub-dir)
1619 "Publish an org file to Texinfo.
1621 FILENAME is the filename of the Org file to be published. PLIST
1622 is the property list for the given project. PUB-DIR is the
1623 publishing directory.
1625 Return output file name."
1626 (org-publish-org-to 'texinfo filename ".texi" plist pub-dir))
1628 ;;;###autoload
1629 (defun org-texinfo-convert-region-to-texinfo ()
1630 "Assume the current region has Org syntax, and convert it to Texinfo.
1631 This can be used in any buffer. For example, you can write an
1632 itemized list in Org syntax in an Texinfo buffer and use this
1633 command to convert it."
1634 (interactive)
1635 (org-export-replace-region-by 'texinfo))
1637 (defun org-texinfo-compile (file)
1638 "Compile a texinfo file.
1640 FILE is the name of the file being compiled. Processing is done
1641 through the command specified in `org-texinfo-info-process',
1642 which see. Output is redirected to \"*Org INFO Texinfo Output*\"
1643 buffer.
1645 Return INFO file name or an error if it couldn't be produced."
1646 (message "Processing Texinfo file %s..." file)
1647 (let* ((log-name "*Org INFO Texinfo Output*")
1648 (log (get-buffer-create log-name))
1649 (output
1650 (org-compile-file file org-texinfo-info-process "info"
1651 (format "See %S for details" log-name)
1652 log)))
1653 (when org-texinfo-remove-logfiles
1654 (let ((base (file-name-sans-extension output)))
1655 (dolist (ext org-texinfo-logfiles-extensions)
1656 (let ((file (concat base "." ext)))
1657 (when (file-exists-p file) (delete-file file))))))
1658 (message "Process completed.")
1659 output))
1662 (provide 'ox-texinfo)
1664 ;; Local variables:
1665 ;; generated-autoload-file: "org-loaddefs.el"
1666 ;; End:
1668 ;;; ox-texinfo.el ends here