ORG-NEWS: Backport commit 62803a2ef from Emacs
[org-mode.git] / lisp / ox-texinfo.el
blob35927d9953310a0d79acf92998df1c83676e8623
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 . verb))
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' and
305 `code'. For the former, Org will use \"@verb\" to create
306 a format string and select a delimiter character that isn't in
307 the string. For the latter, Org will use \"@code\" to typeset
308 and try to protect special characters.
310 If no association can be found for a given markup, text will be
311 returned as-is."
312 :group 'org-export-texinfo
313 :type 'alist
314 :options '(bold code italic strike-through underscore verbatim))
316 ;;;; Drawers
318 (defcustom org-texinfo-format-drawer-function (lambda (_name contents) contents)
319 "Function called to format a drawer in Texinfo code.
321 The function must accept two parameters:
322 NAME the drawer name, like \"LOGBOOK\"
323 CONTENTS the contents of the drawer.
325 The function should return the string to be exported.
327 The default function simply returns the value of CONTENTS."
328 :group 'org-export-texinfo
329 :version "24.4"
330 :package-version '(Org . "8.2")
331 :type 'function)
333 ;;;; Inlinetasks
335 (defcustom org-texinfo-format-inlinetask-function
336 'org-texinfo-format-inlinetask-default-function
337 "Function called to format an inlinetask in Texinfo code.
339 The function must accept six parameters:
340 TODO the todo keyword, as a string
341 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
342 PRIORITY the inlinetask priority, as a string
343 NAME the inlinetask name, as a string.
344 TAGS the inlinetask tags, as a list of strings.
345 CONTENTS the contents of the inlinetask, as a string.
347 The function should return the string to be exported."
348 :group 'org-export-texinfo
349 :type 'function)
351 ;;;; Compilation
353 (defcustom org-texinfo-info-process '("makeinfo %f")
354 "Commands to process a Texinfo file to an INFO file.
356 This is a list of strings, each of them will be given to the
357 shell as a command. %f in the command will be replaced by the
358 relative file name, %F by the absolute file name, %b by the file
359 base name (i.e. without directory and extension parts), %o by the
360 base directory of the file and %O by the absolute file name of
361 the output file."
362 :group 'org-export-texinfo
363 :type '(repeat :tag "Shell command sequence"
364 (string :tag "Shell command")))
366 (defcustom org-texinfo-logfiles-extensions
367 '("aux" "toc" "cp" "fn" "ky" "pg" "tp" "vr")
368 "The list of file extensions to consider as Texinfo logfiles.
369 The logfiles will be remove if `org-texinfo-remove-logfiles' is
370 non-nil."
371 :group 'org-export-texinfo
372 :type '(repeat (string :tag "Extension")))
374 (defcustom org-texinfo-remove-logfiles t
375 "Non-nil means remove the logfiles produced by compiling a Texinfo file.
376 By default, logfiles are files with these extensions: .aux, .toc,
377 .cp, .fn, .ky, .pg and .tp. To define the set of logfiles to remove,
378 set `org-texinfo-logfiles-extensions'."
379 :group 'org-export-latex
380 :type 'boolean)
382 ;;; Constants
384 (defconst org-texinfo-max-toc-depth 4
385 "Maximum depth for creation of detailed menu listings.
386 Beyond this depth, Texinfo will not recognize the nodes and will
387 cause errors. Left as a constant in case this value ever
388 changes.")
390 (defconst org-texinfo-supported-coding-systems
391 '("US-ASCII" "UTF-8" "ISO-8859-15" "ISO-8859-1" "ISO-8859-2" "koi8-r" "koi8-u")
392 "List of coding systems supported by Texinfo, as strings.
393 Specified coding system will be matched against these strings.
394 If two strings share the same prefix (e.g. \"ISO-8859-1\" and
395 \"ISO-8859-15\"), the most specific one has to be listed first.")
397 (defconst org-texinfo-inline-image-rules
398 (list (cons "file"
399 (regexp-opt '("eps" "pdf" "png" "jpg" "jpeg" "gif" "svg"))))
400 "Rules characterizing image files that can be inlined.")
403 ;;; Internal Functions
405 (defun org-texinfo--filter-section-blank-lines (headline _backend _info)
406 "Filter controlling number of blank lines after a section."
407 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" "\n\n" headline))
409 (defun org-texinfo--normalize-headlines (tree _backend info)
410 "Normalize headlines in TREE.
412 BACK-END is the symbol specifying back-end used for export. INFO
413 is a plist used as a communication channel.
415 Make sure every headline in TREE contains a section, since those
416 are required to install a menu. Also put exactly one blank line
417 at the end of each section.
419 Return new tree."
420 (org-element-map tree 'headline
421 (lambda (hl)
422 (org-element-put-property hl :post-blank 1)
423 (let ((contents (org-element-contents hl)))
424 (when contents
425 (let ((first (org-element-map contents '(headline section)
426 #'identity info t)))
427 (unless (eq (org-element-type first) 'section)
428 (apply #'org-element-set-contents
430 (cons `(section (:parent ,hl)) contents)))))))
431 info)
432 tree)
434 (defun org-texinfo--find-verb-separator (s)
435 "Return a character not used in string S.
436 This is used to choose a separator for constructs like \\verb."
437 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
438 (cl-loop for c across ll
439 when (not (string-match (regexp-quote (char-to-string c)) s))
440 return (char-to-string c))))
442 (defun org-texinfo--text-markup (text markup _info)
443 "Format TEXT depending on MARKUP text markup.
444 INFO is a plist used as a communication channel. See
445 `org-texinfo-text-markup-alist' for details."
446 (pcase (cdr (assq markup org-texinfo-text-markup-alist))
447 ;; No format string: Return raw text.
448 (`nil text)
449 (`verb
450 (let ((separator (org-texinfo--find-verb-separator text)))
451 (concat "@verb{" separator text separator "}")))
452 (`code
453 (format "@code{%s}" (replace-regexp-in-string "[@{}]" "@\\&" text)))
454 ;; Else use format string.
455 (fmt (format fmt text))))
457 (defun org-texinfo--get-node (datum info)
458 "Return node or anchor associated to DATUM.
459 DATUM is an element or object. INFO is a plist used as
460 a communication channel. The function guarantees the node or
461 anchor name is unique."
462 (let ((cache (plist-get info :texinfo-node-cache)))
463 (or (cdr (assq datum cache))
464 (let* ((salt 0)
465 (basename
466 (org-texinfo--sanitize-node
467 (if (eq (org-element-type datum) 'headline)
468 (org-texinfo--sanitize-title
469 (org-export-get-alt-title datum info) info)
470 (org-export-get-reference datum info))))
471 (name basename))
472 ;; Ensure NAME is unique and not reserved node name "Top".
473 (while (or (equal name "Top") (rassoc name cache))
474 (setq name (concat basename (format " %d" (cl-incf salt)))))
475 (plist-put info :texinfo-node-cache (cons (cons datum name) cache))
476 name))))
478 (defun org-texinfo--sanitize-node (title)
479 "Bend string TITLE to node line requirements.
480 Trim string and collapse multiple whitespace characters as they
481 are not significant. Replace leading left parenthesis, when
482 followed by a right parenthesis, with a square bracket. Remove
483 periods, commas and colons."
484 (org-trim
485 (replace-regexp-in-string
486 "[ \t]+" " "
487 (replace-regexp-in-string
488 "[:,.]" ""
489 (replace-regexp-in-string "\\`(\\(.*?)\\)" "[\\1" title)))))
491 (defun org-texinfo--sanitize-title (title info)
492 "Make TITLE suitable as a section name.
493 TITLE is a string or a secondary string. INFO is the current
494 export state, as a plist."
495 (org-export-data-with-backend
496 title
497 (org-export-create-backend
498 :parent 'texinfo
499 :transcoders '((footnote-reference . ignore)
500 (link . (lambda (object c i) c))
501 (radio-target . (lambda (object c i) c))
502 (target . ignore)))
503 info))
505 (defun org-texinfo--sanitize-content (text)
506 "Escape special characters in string TEXT.
507 Special characters are: @ { }"
508 (replace-regexp-in-string "[@{}]" "@\\&" text))
510 (defun org-texinfo--wrap-float (value info &optional type label caption short)
511 "Wrap string VALUE within a @float command.
512 INFO is the current export state, as a plist. TYPE is float
513 type, as a string. LABEL is the cross reference label for the
514 float, as a string. CAPTION and SHORT are, respectively, the
515 caption and shortcaption used for the float, as secondary
516 strings (e.g., returned by `org-export-get-caption')."
517 (let* ((backend
518 (org-export-create-backend
519 :parent 'texinfo
520 :transcoders '((link . (lambda (object c i) c))
521 (radio-target . (lambda (object c i) c))
522 (target . ignore))))
523 (short-backend
524 (org-export-create-backend
525 :parent 'texinfo
526 :transcoders '((footnote-reference . ignore)
527 (inline-src-block . ignore)
528 (link . (lambda (object c i) c))
529 (radio-target . (lambda (object c i) c))
530 (target . ignore)
531 (verbatim . ignore))))
532 (short-str
533 (if (and short caption)
534 (format "@shortcaption{%s}\n"
535 (org-export-data-with-backend short short-backend info))
536 ""))
537 (caption-str
538 (if (or short caption)
539 (format "@caption{%s}\n"
540 (org-export-data-with-backend
541 (or caption short)
542 (if (equal short-str "") short-backend backend)
543 info))
544 "")))
545 (format "@float %s%s\n%s\n%s%s@end float"
546 type (if label (concat "," label) "") value caption-str short-str)))
548 ;;; Template
550 (defun org-texinfo-template (contents info)
551 "Return complete document string after Texinfo conversion.
552 CONTENTS is the transcoded contents string. INFO is a plist
553 holding export options."
554 (let ((title (org-export-data (plist-get info :title) info))
555 ;; Copying data is the contents of the first headline in
556 ;; parse tree with a non-nil copying property.
557 (copying (org-element-map (plist-get info :parse-tree) 'headline
558 (lambda (hl)
559 (and (org-not-nil (org-element-property :COPYING hl))
560 (org-element-contents hl)))
561 info t)))
562 (concat
563 "\\input texinfo @c -*- texinfo -*-\n"
564 "@c %**start of header\n"
565 (let ((file (or (plist-get info :texinfo-filename)
566 (let ((f (plist-get info :output-file)))
567 (and f (concat (file-name-sans-extension f) ".info"))))))
568 (and file (format "@setfilename %s\n" file)))
569 (format "@settitle %s\n" title)
570 ;; Insert class-defined header.
571 (org-element-normalize-string
572 (let ((header (nth 1 (assoc (plist-get info :texinfo-class)
573 org-texinfo-classes)))
574 (coding
575 (catch 'coding-system
576 (let ((case-fold-search t)
577 (name (symbol-name (or org-texinfo-coding-system
578 buffer-file-coding-system))))
579 (dolist (system org-texinfo-supported-coding-systems "UTF-8")
580 (when (string-match-p (regexp-quote system) name)
581 (throw 'coding-system system))))))
582 (language (plist-get info :language))
583 (case-fold-search nil))
584 ;; Auto coding system.
585 (replace-regexp-in-string
586 "^@documentencoding \\(AUTO\\)$"
587 coding
588 (replace-regexp-in-string
589 "^@documentlanguage \\(AUTO\\)$" language header t nil 1) t nil 1)))
590 ;; Additional header options set by #+TEXINFO_HEADER.
591 (let ((texinfo-header (plist-get info :texinfo-header)))
592 (and texinfo-header (org-element-normalize-string texinfo-header)))
593 "@c %**end of header\n\n"
594 ;; Additional options set by #+TEXINFO_POST_HEADER.
595 (let ((texinfo-post-header (plist-get info :texinfo-post-header)))
596 (and texinfo-post-header
597 (org-element-normalize-string texinfo-post-header)))
598 ;; Copying.
599 (and copying
600 (format "@copying\n%s@end copying\n\n"
601 (org-element-normalize-string
602 (org-export-data copying info))))
603 ;; Info directory information. Only supply if both title and
604 ;; category are provided.
605 (let ((dircat (plist-get info :texinfo-dircat))
606 (dirtitle
607 (let ((title (plist-get info :texinfo-dirtitle)))
608 (and title
609 (string-match "^\\(?:\\* \\)?\\(.*?\\)\\(\\.\\)?$" title)
610 (format "* %s." (match-string 1 title))))))
611 (when (and dircat dirtitle)
612 (concat "@dircategory " dircat "\n"
613 "@direntry\n"
614 (let ((dirdesc
615 (let ((desc (plist-get info :texinfo-dirdesc)))
616 (cond ((not desc) nil)
617 ((string-suffix-p "." desc) desc)
618 (t (concat desc "."))))))
619 (if dirdesc (format "%-23s %s" dirtitle dirdesc) dirtitle))
620 "\n"
621 "@end direntry\n\n")))
622 ;; Title
623 "@finalout\n"
624 "@titlepage\n"
625 (when (plist-get info :with-title)
626 (concat
627 (format "@title %s\n"
628 (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. Also include contents
655 ;; from the first section of the document.
656 "@ifnottex\n"
657 "@node Top\n"
658 (format "@top %s\n" title)
659 (let* ((first-section
660 (org-element-map (plist-get info :parse-tree) 'section
661 #'identity info t '(headline)))
662 (top-contents
663 (org-export-data (org-element-contents first-section) info)))
664 (and (org-string-nw-p top-contents) (concat "\n" top-contents)))
665 "@end ifnottex\n\n"
666 ;; Menu.
667 (org-texinfo-make-menu (plist-get info :parse-tree) info 'master)
668 "\n"
669 ;; Document's body.
670 contents "\n"
671 ;; Creator.
672 (and (plist-get info :with-creator)
673 (concat (plist-get info :creator) "\n"))
674 ;; Document end.
675 "@bye")))
679 ;;; Transcode Functions
681 ;;;; Bold
683 (defun org-texinfo-bold (_bold contents info)
684 "Transcode BOLD from Org to Texinfo.
685 CONTENTS is the text with bold markup. INFO is a plist holding
686 contextual information."
687 (org-texinfo--text-markup contents 'bold info))
689 ;;;; Center Block
691 (defun org-texinfo-center-block (_center-block contents _info)
692 "Transcode a CENTER-BLOCK element from Org to Texinfo.
693 CONTENTS holds the contents of the block. INFO is a plist used
694 as a communication channel."
695 contents)
697 ;;;; Clock
699 (defun org-texinfo-clock (clock _contents info)
700 "Transcode a CLOCK element from Org to Texinfo.
701 CONTENTS is nil. INFO is a plist holding contextual
702 information."
703 (concat
704 "@noindent"
705 (format "@strong{%s} " org-clock-string)
706 (format (plist-get info :texinfo-inactive-timestamp-format)
707 (concat (org-timestamp-translate (org-element-property :value clock))
708 (let ((time (org-element-property :duration clock)))
709 (and time (format " (%s)" time)))))
710 "@*"))
712 ;;;; Code
714 (defun org-texinfo-code (code _contents info)
715 "Transcode a CODE object from Org to Texinfo.
716 CONTENTS is nil. INFO is a plist used as a communication
717 channel."
718 (org-texinfo--text-markup (org-element-property :value code) 'code info))
720 ;;;; Drawer
722 (defun org-texinfo-drawer (drawer contents info)
723 "Transcode a DRAWER element from Org to Texinfo.
724 CONTENTS holds the contents of the block. INFO is a plist
725 holding contextual information."
726 (let* ((name (org-element-property :drawer-name drawer))
727 (output (funcall (plist-get info :texinfo-format-drawer-function)
728 name contents)))
729 output))
731 ;;;; Dynamic Block
733 (defun org-texinfo-dynamic-block (_dynamic-block contents _info)
734 "Transcode a DYNAMIC-BLOCK element from Org to Texinfo.
735 CONTENTS holds the contents of the block. INFO is a plist
736 holding contextual information."
737 contents)
739 ;;;; Entity
741 (defun org-texinfo-entity (entity _contents _info)
742 "Transcode an ENTITY object from Org to Texinfo."
743 ;; Since there is not specific Texinfo entry in entities, use
744 ;; Texinfo-specific commands whenever possible, and fallback to
745 ;; UTF-8 otherwise.
746 (pcase (org-element-property :name entity)
747 ("AElig" "@AE{}")
748 ("aelig" "@ae{}")
749 ((or "bull" "bullet") "@bullet{}")
750 ("copy" "@copyright{}")
751 ("deg" "@textdegree{}")
752 ((or "dots" "hellip") "@dots{}")
753 ("equiv" "@equiv{}")
754 ((or "euro" "EUR") "@euro{}")
755 ((or "ge" "geq") "@geq{}")
756 ("laquo" "@guillemetleft{}")
757 ("iexcl" "@exclamdown{}")
758 ("imath" "@dotless{i}")
759 ("iquest" "@questiondown{}")
760 ("jmath" "@dotless{j}")
761 ((or "le" "leq") "@leq{}")
762 ("lsaquo" "@guilsinglleft{}")
763 ("mdash" "---")
764 ("minus" "@minus{}")
765 ("nbsp" "@tie{}")
766 ("ndash" "--")
767 ("OElig" "@OE{}")
768 ("oelig" "@oe{}")
769 ("ordf" "@ordf{}")
770 ("ordm" "@ordm{}")
771 ("pound" "@pound{}")
772 ("raquo" "@guillemetright{}")
773 ((or "rArr" "Rightarrow") "@result{}")
774 ("reg" "@registeredsymbol{}")
775 ((or "rightarrow" "to" "rarr") "@arrow{}")
776 ("rsaquo" "@guilsinglright{}")
777 ("thorn" "@th{}")
778 ("THORN" "@TH{}")
779 ((and (pred (string-prefix-p "_")) name) ;spacing entities
780 (format "@w{%s}" (substring name 1)))
781 (_ (org-element-property :utf-8 entity))))
783 ;;;; Example Block
785 (defun org-texinfo-example-block (example-block _contents info)
786 "Transcode an EXAMPLE-BLOCK element from Org to Texinfo.
787 CONTENTS is nil. INFO is a plist holding contextual
788 information."
789 (format "@verbatim\n%s@end verbatim"
790 (org-export-format-code-default example-block info)))
792 ;;; Export Block
794 (defun org-texinfo-export-block (export-block _contents _info)
795 "Transcode a EXPORT-BLOCK element from Org to Texinfo.
796 CONTENTS is nil. INFO is a plist holding contextual information."
797 (when (string= (org-element-property :type export-block) "TEXINFO")
798 (org-remove-indentation (org-element-property :value export-block))))
800 ;;; Export Snippet
802 (defun org-texinfo-export-snippet (export-snippet _contents _info)
803 "Transcode a EXPORT-SNIPPET object from Org to Texinfo.
804 CONTENTS is nil. INFO is a plist holding contextual information."
805 (when (eq (org-export-snippet-backend export-snippet) 'texinfo)
806 (org-element-property :value export-snippet)))
808 ;;;; Fixed Width
810 (defun org-texinfo-fixed-width (fixed-width _contents _info)
811 "Transcode a FIXED-WIDTH element from Org to Texinfo.
812 CONTENTS is nil. INFO is a plist holding contextual information."
813 (format "@example\n%s@end example"
814 (org-remove-indentation
815 (org-texinfo--sanitize-content
816 (org-element-property :value fixed-width)))))
818 ;;;; Footnote Reference
820 (defun org-texinfo-footnote-reference (footnote _contents info)
821 "Create a footnote reference for FOOTNOTE.
823 FOOTNOTE is the footnote to define. CONTENTS is nil. INFO is a
824 plist holding contextual information."
825 (let ((def (org-export-get-footnote-definition footnote info)))
826 (format "@footnote{%s}"
827 (org-trim (org-export-data def info)))))
829 ;;;; Headline
831 (defun org-texinfo-headline (headline contents info)
832 "Transcode a HEADLINE element from Org to Texinfo.
833 CONTENTS holds the contents of the headline. INFO is a plist
834 holding contextual information."
835 (let* ((class (plist-get info :texinfo-class))
836 (level (org-export-get-relative-level headline info))
837 (numberedp (org-export-numbered-headline-p headline info))
838 (class-sectioning (assoc class (plist-get info :texinfo-classes)))
839 ;; Find the index type, if any.
840 (index (org-element-property :INDEX headline))
841 ;; Create node info, to insert it before section formatting.
842 ;; Use custom menu title if present.
843 (node (format "@node %s\n" (org-texinfo--get-node headline info)))
844 ;; Section formatting will set two placeholders: one for the
845 ;; title and the other for the contents.
846 (section-fmt
847 (if (org-not-nil (org-element-property :APPENDIX headline))
848 "@appendix %s\n%s"
849 (let ((sec (if (and (symbolp (nth 2 class-sectioning))
850 (fboundp (nth 2 class-sectioning)))
851 (funcall (nth 2 class-sectioning) level numberedp)
852 (nth (1+ level) class-sectioning))))
853 (cond
854 ;; No section available for that LEVEL.
855 ((not sec) nil)
856 ;; Section format directly returned by a function.
857 ((stringp sec) sec)
858 ;; (numbered-section . unnumbered-section)
859 ((not (consp (cdr sec)))
860 (concat (if (or index (not numberedp)) (cdr sec) (car sec))
861 "\n%s"))))))
862 (todo
863 (and (plist-get info :with-todo-keywords)
864 (let ((todo (org-element-property :todo-keyword headline)))
865 (and todo (org-export-data todo info)))))
866 (todo-type (and todo (org-element-property :todo-type headline)))
867 (tags (and (plist-get info :with-tags)
868 (org-export-get-tags headline info)))
869 (priority (and (plist-get info :with-priority)
870 (org-element-property :priority headline)))
871 (text (org-texinfo--sanitize-title
872 (org-element-property :title headline) info))
873 (full-text (funcall (plist-get info :texinfo-format-headline-function)
874 todo todo-type priority text tags))
875 (contents (if (org-string-nw-p contents) (concat "\n" contents) "")))
876 (cond
877 ;; Case 1: This is a footnote section: ignore it.
878 ((org-element-property :footnote-section-p headline) nil)
879 ;; Case 2: This is the `copying' section: ignore it
880 ;; This is used elsewhere.
881 ((org-not-nil (org-element-property :COPYING headline)) nil)
882 ;; Case 3: An index. If it matches one of the known indexes,
883 ;; print it as such following the contents, otherwise
884 ;; print the contents and leave the index up to the user.
885 (index
886 (concat node
887 (format
888 section-fmt
889 full-text
890 (concat contents
891 (and (member index '("cp" "fn" "ky" "pg" "tp" "vr"))
892 (concat "\n@printindex " index))))))
893 ;; Case 4: This is a deep sub-tree: export it as a list item.
894 ;; Also export as items headlines for which no section
895 ;; format has been found.
896 ((or (not section-fmt) (org-export-low-level-p headline info))
897 ;; Build the real contents of the sub-tree.
898 (concat (and (org-export-first-sibling-p headline info)
899 (format "@%s\n" (if numberedp 'enumerate 'itemize)))
900 "@item\n" full-text "\n"
901 contents
902 (if (org-export-last-sibling-p headline info)
903 (format "@end %s" (if numberedp 'enumerate 'itemize))
904 "\n")))
905 ;; Case 5: Standard headline. Export it as a section.
906 (t (concat node (format section-fmt full-text contents))))))
908 (defun org-texinfo-format-headline-default-function
909 (todo _todo-type priority text tags)
910 "Default format function for a headline.
911 See `org-texinfo-format-headline-function' for details."
912 (concat (when todo (format "@strong{%s} " todo))
913 (when priority (format "@emph{#%s} " priority))
914 text
915 (when tags (format " :%s:" (mapconcat 'identity tags ":")))))
917 ;;;; Inline Src Block
919 (defun org-texinfo-inline-src-block (inline-src-block _contents _info)
920 "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
921 CONTENTS holds the contents of the item. INFO is a plist holding
922 contextual information."
923 (let* ((code (org-element-property :value inline-src-block))
924 (separator (org-texinfo--find-verb-separator code)))
925 (concat "@verb{" separator code separator "}")))
927 ;;;; Inlinetask
929 (defun org-texinfo-inlinetask (inlinetask contents info)
930 "Transcode an INLINETASK element from Org to Texinfo.
931 CONTENTS holds the contents of the block. INFO is a plist
932 holding contextual information."
933 (let ((title (org-export-data (org-element-property :title inlinetask) info))
934 (todo (and (plist-get info :with-todo-keywords)
935 (let ((todo (org-element-property :todo-keyword inlinetask)))
936 (and todo (org-export-data todo info)))))
937 (todo-type (org-element-property :todo-type inlinetask))
938 (tags (and (plist-get info :with-tags)
939 (org-export-get-tags inlinetask info)))
940 (priority (and (plist-get info :with-priority)
941 (org-element-property :priority inlinetask))))
942 (funcall (plist-get info :texinfo-format-inlinetask-function)
943 todo todo-type priority title tags contents)))
945 (defun org-texinfo-format-inlinetask-default-function
946 (todo _todo-type priority title tags contents)
947 "Default format function for a inlinetasks.
948 See `org-texinfo-format-inlinetask-function' for details."
949 (let ((full-title
950 (concat (when todo (format "@strong{%s} " todo))
951 (when priority (format "#%c " priority))
952 title
953 (when tags (format ":%s:" (mapconcat #'identity tags ":"))))))
954 (format "@center %s\n\n%s\n" full-title contents)))
956 ;;;; Italic
958 (defun org-texinfo-italic (_italic contents info)
959 "Transcode ITALIC from Org to Texinfo.
960 CONTENTS is the text with italic markup. INFO is a plist holding
961 contextual information."
962 (org-texinfo--text-markup contents 'italic info))
964 ;;;; Item
966 (defun org-texinfo-item (item contents info)
967 "Transcode an ITEM element from Org to Texinfo.
968 CONTENTS holds the contents of the item. INFO is a plist holding
969 contextual information."
970 (format "@item%s\n%s"
971 (let ((tag (org-element-property :tag item)))
972 (if tag (concat " " (org-export-data tag info)) ""))
973 (or contents "")))
975 ;;;; Keyword
977 (defun org-texinfo-keyword (keyword _contents info)
978 "Transcode a KEYWORD element from Org to Texinfo.
979 CONTENTS is nil. INFO is a plist holding contextual information."
980 (let ((value (org-element-property :value keyword)))
981 (pcase (org-element-property :key keyword)
982 ("TEXINFO" value)
983 ("CINDEX" (format "@cindex %s" value))
984 ("FINDEX" (format "@findex %s" value))
985 ("KINDEX" (format "@kindex %s" value))
986 ("PINDEX" (format "@pindex %s" value))
987 ("TINDEX" (format "@tindex %s" value))
988 ("VINDEX" (format "@vindex %s" value))
989 ("TOC"
990 (cond ((string-match-p "\\<tables\\>" value)
991 (concat "@listoffloats "
992 (org-export-translate "Table" :utf-8 info)))
993 ((string-match-p "\\<listings\\>" value)
994 (concat "@listoffloats "
995 (org-export-translate "Listing" :utf-8 info))))))))
997 ;;;; Line Break
999 (defun org-texinfo-line-break (_line-break _contents _info)
1000 "Transcode a LINE-BREAK object from Org to Texinfo.
1001 CONTENTS is nil. INFO is a plist holding contextual information."
1002 "@*\n")
1004 ;;;; Link
1006 (defun org-texinfo--@ref (datum description info)
1007 "Return @ref command for element or object DATUM.
1008 DESCRIPTION is the name of the section to print, as a string."
1009 (let ((node-name (org-texinfo--get-node datum info))
1010 ;; Sanitize DESCRIPTION for cross-reference use. In
1011 ;; particular, remove colons as they seem to cause (even
1012 ;; within @asis{...} to the Texinfo reader.
1013 (title (replace-regexp-in-string
1014 "[ \t]*:+" ""
1015 (replace-regexp-in-string "," "@comma{}" description))))
1016 (if (equal title node-name)
1017 (format "@ref{%s}" node-name)
1018 (format "@ref{%s, , %s}" node-name title))))
1020 (defun org-texinfo-link (link desc info)
1021 "Transcode a LINK object from Org to Texinfo.
1022 DESC is the description part of the link, or the empty string.
1023 INFO is a plist holding contextual information. See
1024 `org-export-data'."
1025 (let* ((type (org-element-property :type link))
1026 (raw-path (org-element-property :path link))
1027 ;; Ensure DESC really exists, or set it to nil.
1028 (desc (and (not (string= desc "")) desc))
1029 (path (cond
1030 ((member type '("http" "https" "ftp"))
1031 (concat type ":" raw-path))
1032 ((string= type "file") (org-export-file-uri raw-path))
1033 (t raw-path))))
1034 (cond
1035 ((org-export-custom-protocol-maybe link desc 'texinfo))
1036 ((org-export-inline-image-p link org-texinfo-inline-image-rules)
1037 (org-texinfo--inline-image link info))
1038 ((equal type "radio")
1039 (let ((destination (org-export-resolve-radio-link link info)))
1040 (if (not destination) desc
1041 (org-texinfo--@ref destination desc info))))
1042 ((member type '("custom-id" "id" "fuzzy"))
1043 (let ((destination
1044 (if (equal type "fuzzy")
1045 (org-export-resolve-fuzzy-link link info)
1046 (org-export-resolve-id-link link info))))
1047 (pcase (org-element-type destination)
1048 (`nil
1049 (format org-texinfo-link-with-unknown-path-format
1050 (org-texinfo--sanitize-content path)))
1051 ;; Id link points to an external file.
1052 (`plain-text
1053 (if desc (format "@uref{file://%s,%s}" destination desc)
1054 (format "@uref{file://%s}" destination)))
1055 ((or `headline
1056 ;; Targets within headlines cannot be turned into
1057 ;; @anchor{}, so we refer to the headline parent
1058 ;; directly.
1059 (and `target
1060 (guard (eq 'headline
1061 (org-element-type
1062 (org-element-property :parent destination))))))
1063 (let ((headline (org-element-lineage destination '(headline) t)))
1064 (org-texinfo--@ref
1065 headline
1066 (or desc (org-texinfo--sanitize-title
1067 (org-element-property :title headline) info))
1068 info)))
1070 (org-texinfo--@ref
1071 destination
1072 (or desc
1073 (pcase (org-export-get-ordinal destination info)
1074 ((and (pred integerp) n) (number-to-string n))
1075 ((and (pred consp) n) (mapconcat #'number-to-string n "."))
1076 (_ "???")))
1077 info))))) ;cannot guess the description
1078 ((equal type "info")
1079 (let* ((info-path (split-string path "[:#]"))
1080 (info-manual (car info-path))
1081 (info-node (or (cadr info-path) "Top"))
1082 (title (or desc "")))
1083 (format "@ref{%s,%s,,%s,}" info-node title info-manual)))
1084 ((string= type "mailto")
1085 (format "@email{%s}"
1086 (concat (org-texinfo--sanitize-content path)
1087 (and desc (concat ", " desc)))))
1088 ;; External link with a description part.
1089 ((and path desc) (format "@uref{%s, %s}" path desc))
1090 ;; External link without a description part.
1091 (path (format "@uref{%s}" path))
1092 ;; No path, only description. Try to do something useful.
1094 (format (plist-get info :texinfo-link-with-unknown-path-format) desc)))))
1096 (defun org-texinfo--inline-image (link info)
1097 "Return Texinfo code for an inline image.
1098 LINK is the link pointing to the inline image. INFO is the
1099 current state of the export, as a plist."
1100 (let* ((parent (org-export-get-parent-element link))
1101 (label (and (org-element-property :name parent)
1102 (org-texinfo--get-node parent info)))
1103 (caption (org-export-get-caption parent))
1104 (shortcaption (org-export-get-caption parent t))
1105 (path (org-element-property :path link))
1106 (filename
1107 (file-name-sans-extension
1108 (if (file-name-absolute-p path) (expand-file-name path) path)))
1109 (extension (file-name-extension path))
1110 (attributes (org-export-read-attribute :attr_texinfo parent))
1111 (height (or (plist-get attributes :height) ""))
1112 (width (or (plist-get attributes :width) ""))
1113 (alt (or (plist-get attributes :alt) ""))
1114 (image (format "@image{%s,%s,%s,%s,%s}"
1115 filename width height alt extension)))
1116 (cond ((or caption shortcaption)
1117 (org-texinfo--wrap-float image
1118 info
1119 (org-export-translate "Figure" :utf-8 info)
1120 label
1121 caption
1122 shortcaption))
1123 (label (concat "@anchor{" label "}\n" image))
1124 (t image))))
1127 ;;;; Menu
1129 (defun org-texinfo-make-menu (scope info &optional master)
1130 "Create the menu for inclusion in the Texinfo document.
1132 SCOPE is a headline or a full parse tree. INFO is the
1133 communication channel, as a plist.
1135 When optional argument MASTER is non-nil, generate a master menu,
1136 including detailed node listing."
1137 (let ((menu (org-texinfo--build-menu scope info)))
1138 (when (org-string-nw-p menu)
1139 (org-element-normalize-string
1140 (format
1141 "@menu\n%s@end menu"
1142 (concat menu
1143 (when master
1144 (let ((detailmenu
1145 (org-texinfo--build-menu
1146 scope info
1147 (let ((toc-depth (plist-get info :with-toc)))
1148 (if (wholenump toc-depth) toc-depth
1149 org-texinfo-max-toc-depth)))))
1150 (when (org-string-nw-p detailmenu)
1151 (concat "\n@detailmenu\n"
1152 "--- The Detailed Node Listing ---\n\n"
1153 detailmenu
1154 "@end detailmenu\n"))))))))))
1156 (defun org-texinfo--build-menu (scope info &optional level)
1157 "Build menu for entries within SCOPE.
1158 SCOPE is a headline or a full parse tree. INFO is a plist
1159 containing contextual information. When optional argument LEVEL
1160 is an integer, build the menu recursively, down to this depth."
1161 (cond
1162 ((not level)
1163 (org-texinfo--format-entries (org-texinfo--menu-entries scope info) info))
1164 ((zerop level) nil)
1166 (org-element-normalize-string
1167 (mapconcat
1168 (lambda (h)
1169 (let ((entries (org-texinfo--menu-entries h info)))
1170 (when entries
1171 (concat
1172 (format "%s\n\n%s\n"
1173 (org-export-data (org-export-get-alt-title h info) info)
1174 (org-texinfo--format-entries entries info))
1175 (org-texinfo--build-menu h info (1- level))))))
1176 (org-texinfo--menu-entries scope info) "\n")))))
1178 (defun org-texinfo--format-entries (entries info)
1179 "Format all direct menu entries in SCOPE, as a string.
1180 SCOPE is either a headline or a full Org document. INFO is
1181 a plist containing contextual information."
1182 (org-element-normalize-string
1183 (mapconcat
1184 (lambda (h)
1185 (let* ((title
1186 ;; Colons are used as a separator between title and node
1187 ;; name. Remove them.
1188 (replace-regexp-in-string
1189 "[ \t]+:+" ""
1190 (org-texinfo--sanitize-title
1191 (org-export-get-alt-title h info) info)))
1192 (node (org-texinfo--get-node h info))
1193 (entry (concat "* " title ":"
1194 (if (string= title node) ":"
1195 (concat " " node ". "))))
1196 (desc (org-element-property :DESCRIPTION h)))
1197 (if (not desc) entry
1198 (format (format "%%-%ds %%s" org-texinfo-node-description-column)
1199 entry desc))))
1200 entries "\n")))
1202 (defun org-texinfo--menu-entries (scope info)
1203 "List direct children in SCOPE needing a menu entry.
1204 SCOPE is a headline or a full parse tree. INFO is a plist
1205 holding contextual information."
1206 (let* ((cache (or (plist-get info :texinfo-entries-cache)
1207 (plist-get (plist-put info :texinfo-entries-cache
1208 (make-hash-table :test #'eq))
1209 :texinfo-entries-cache)))
1210 (cached-entries (gethash scope cache 'no-cache)))
1211 (if (not (eq cached-entries 'no-cache)) cached-entries
1212 (puthash scope
1213 (org-element-map (org-element-contents scope) 'headline
1214 (lambda (h)
1215 (and (not (org-not-nil (org-element-property :COPYING h)))
1216 (not (org-element-property :footnote-section-p h))
1217 (not (org-export-low-level-p h info))
1219 info nil 'headline)
1220 cache))))
1222 ;;;; Node Property
1224 (defun org-texinfo-node-property (node-property _contents _info)
1225 "Transcode a NODE-PROPERTY element from Org to Texinfo.
1226 CONTENTS is nil. INFO is a plist holding contextual
1227 information."
1228 (format "%s:%s"
1229 (org-element-property :key node-property)
1230 (let ((value (org-element-property :value node-property)))
1231 (if value (concat " " value) ""))))
1233 ;;;; Paragraph
1235 (defun org-texinfo-paragraph (_paragraph contents _info)
1236 "Transcode a PARAGRAPH element from Org to Texinfo.
1237 CONTENTS is the contents of the paragraph, as a string. INFO is
1238 the plist used as a communication channel."
1239 contents)
1241 ;;;; Plain List
1243 (defun org-texinfo-plain-list (plain-list contents info)
1244 "Transcode a PLAIN-LIST element from Org to Texinfo.
1245 CONTENTS is the contents of the list. INFO is a plist holding
1246 contextual information."
1247 (let* ((attr (org-export-read-attribute :attr_texinfo plain-list))
1248 (indic (let ((i (or (plist-get attr :indic)
1249 (plist-get info :texinfo-def-table-markup))))
1250 ;; Allow indicating commands with missing @ sign.
1251 (if (string-prefix-p "@" i) i (concat "@" i))))
1252 (table-type (plist-get attr :table-type))
1253 (type (org-element-property :type plain-list))
1254 (list-type (cond
1255 ((eq type 'ordered) "enumerate")
1256 ((eq type 'unordered) "itemize")
1257 ((member table-type '("ftable" "vtable")) table-type)
1258 (t "table"))))
1259 (format "@%s\n%s@end %s"
1260 (if (eq type 'descriptive) (concat list-type " " indic) list-type)
1261 contents
1262 list-type)))
1264 ;;;; Plain Text
1266 (defun org-texinfo-plain-text (text info)
1267 "Transcode a TEXT string from Org to Texinfo.
1268 TEXT is the string to transcode. INFO is a plist holding
1269 contextual information."
1270 ;; First protect @, { and }.
1271 (let ((output (org-texinfo--sanitize-content text)))
1272 ;; Activate smart quotes. Be sure to provide original TEXT string
1273 ;; since OUTPUT may have been modified.
1274 (when (plist-get info :with-smart-quotes)
1275 (setq output
1276 (org-export-activate-smart-quotes output :texinfo info text)))
1277 ;; LaTeX into @LaTeX{} and TeX into @TeX{}
1278 (let ((case-fold-search nil))
1279 (setq output (replace-regexp-in-string "\\(?:La\\)?TeX" "@\\&{}" output)))
1280 ;; Convert special strings.
1281 (when (plist-get info :with-special-strings)
1282 (setq output
1283 (replace-regexp-in-string
1284 "\\.\\.\\." "@dots{}"
1285 (replace-regexp-in-string "\\\\-" "@-" output))))
1286 ;; Handle break preservation if required.
1287 (when (plist-get info :preserve-breaks)
1288 (setq output (replace-regexp-in-string
1289 "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" output)))
1290 ;; Return value.
1291 output))
1293 ;;;; Planning
1295 (defun org-texinfo-planning (planning _contents info)
1296 "Transcode a PLANNING element from Org to Texinfo.
1297 CONTENTS is nil. INFO is a plist holding contextual
1298 information."
1299 (concat
1300 "@noindent"
1301 (mapconcat
1302 'identity
1303 (delq nil
1304 (list
1305 (let ((closed (org-element-property :closed planning)))
1306 (when closed
1307 (concat
1308 (format "@strong{%s} " org-closed-string)
1309 (format (plist-get info :texinfo-inactive-timestamp-format)
1310 (org-timestamp-translate closed)))))
1311 (let ((deadline (org-element-property :deadline planning)))
1312 (when deadline
1313 (concat
1314 (format "@strong{%s} " org-deadline-string)
1315 (format (plist-get info :texinfo-active-timestamp-format)
1316 (org-timestamp-translate deadline)))))
1317 (let ((scheduled (org-element-property :scheduled planning)))
1318 (when scheduled
1319 (concat
1320 (format "@strong{%s} " org-scheduled-string)
1321 (format (plist-get info :texinfo-active-timestamp-format)
1322 (org-timestamp-translate scheduled)))))))
1323 " ")
1324 "@*"))
1326 ;;;; Property Drawer
1328 (defun org-texinfo-property-drawer (_property-drawer contents _info)
1329 "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
1330 CONTENTS holds the contents of the drawer. INFO is a plist
1331 holding contextual information."
1332 (and (org-string-nw-p contents)
1333 (format "@verbatim\n%s@end verbatim" contents)))
1335 ;;;; Quote Block
1337 (defun org-texinfo-quote-block (quote-block contents _info)
1338 "Transcode a QUOTE-BLOCK element from Org to Texinfo.
1339 CONTENTS holds the contents of the block. INFO is a plist
1340 holding contextual information."
1341 (let* ((title (org-element-property :name quote-block))
1342 (start-quote (concat "@quotation"
1343 (if title
1344 (format " %s" title)))))
1345 (format "%s\n%s@end quotation" start-quote contents)))
1347 ;;;; Radio Target
1349 (defun org-texinfo-radio-target (radio-target text info)
1350 "Transcode a RADIO-TARGET object from Org to Texinfo.
1351 TEXT is the text of the target. INFO is a plist holding
1352 contextual information."
1353 (format "@anchor{%s}%s"
1354 (org-texinfo--get-node radio-target info)
1355 text))
1357 ;;;; Section
1359 (defun org-texinfo-section (section contents info)
1360 "Transcode a SECTION element from Org to Texinfo.
1361 CONTENTS holds the contents of the section. INFO is a plist
1362 holding contextual information."
1363 (let ((parent (org-export-get-parent-headline section)))
1364 (when parent ;ignore very first section
1365 (org-trim
1366 (concat contents "\n" (org-texinfo-make-menu parent info))))))
1368 ;;;; Special Block
1370 (defun org-texinfo-special-block (special-block contents _info)
1371 "Transcode a SPECIAL-BLOCK element from Org to Texinfo.
1372 CONTENTS holds the contents of the block. INFO is a plist used
1373 as a communication channel."
1374 (let ((opt (org-export-read-attribute :attr_texinfo special-block :options))
1375 (type (org-element-property :type special-block)))
1376 (format "@%s%s\n%s@end %s"
1377 type
1378 (if opt (concat " " opt) "")
1379 (or contents "")
1380 type)))
1382 ;;;; Src Block
1384 (defun org-texinfo-src-block (src-block _contents info)
1385 "Transcode a SRC-BLOCK element from Org to Texinfo.
1386 CONTENTS holds the contents of the item. INFO is a plist holding
1387 contextual information."
1388 (let* ((lisp (string-match-p "lisp"
1389 (org-element-property :language src-block)))
1390 (code (org-texinfo--sanitize-content
1391 (org-export-format-code-default src-block info)))
1392 (value (format
1393 (if lisp "@lisp\n%s@end lisp" "@example\n%s@end example")
1394 code))
1395 (caption (org-export-get-caption src-block))
1396 (shortcaption (org-export-get-caption src-block t)))
1397 (if (not (or caption shortcaption)) value
1398 (org-texinfo--wrap-float value
1399 info
1400 (org-export-translate "Listing" :utf-8 info)
1401 (org-texinfo--get-node src-block info)
1402 caption
1403 shortcaption))))
1405 ;;;; Statistics Cookie
1407 (defun org-texinfo-statistics-cookie (statistics-cookie _contents _info)
1408 "Transcode a STATISTICS-COOKIE object from Org to Texinfo.
1409 CONTENTS is nil. INFO is a plist holding contextual information."
1410 (org-element-property :value statistics-cookie))
1413 ;;;; Strike-through
1415 (defun org-texinfo-strike-through (_strike-through contents info)
1416 "Transcode STRIKE-THROUGH from Org to Texinfo.
1417 CONTENTS is the text with strike-through markup. INFO is a plist
1418 holding contextual information."
1419 (org-texinfo--text-markup contents 'strike-through info))
1421 ;;;; Subscript
1423 (defun org-texinfo-subscript (_subscript contents _info)
1424 "Transcode a SUBSCRIPT object from Org to Texinfo.
1425 CONTENTS is the contents of the object. INFO is a plist holding
1426 contextual information."
1427 (format "@math{_%s}" contents))
1429 ;;;; Superscript
1431 (defun org-texinfo-superscript (_superscript contents _info)
1432 "Transcode a SUPERSCRIPT object from Org to Texinfo.
1433 CONTENTS is the contents of the object. INFO is a plist holding
1434 contextual information."
1435 (format "@math{^%s}" contents))
1437 ;;;; Table
1439 (defun org-texinfo-table (table contents info)
1440 "Transcode a TABLE element from Org to Texinfo.
1441 CONTENTS is the contents of the table. INFO is a plist holding
1442 contextual information."
1443 (if (eq (org-element-property :type table) 'table.el)
1444 (format "@verbatim\n%s@end verbatim"
1445 (org-element-normalize-string
1446 (org-element-property :value table)))
1447 (let* ((col-width (org-export-read-attribute :attr_texinfo table :columns))
1448 (columns
1449 (if col-width (format "@columnfractions %s" col-width)
1450 (org-texinfo-table-column-widths table info)))
1451 (caption (org-export-get-caption table))
1452 (shortcaption (org-export-get-caption table t))
1453 (table-str (format "@multitable %s\n%s@end multitable"
1454 columns
1455 contents)))
1456 (if (not (or caption shortcaption)) table-str
1457 (org-texinfo--wrap-float table-str
1458 info
1459 (org-export-translate "Table" :utf-8 info)
1460 (org-texinfo--get-node table info)
1461 caption
1462 shortcaption)))))
1464 (defun org-texinfo-table-column-widths (table info)
1465 "Determine the largest table cell in each column to process alignment.
1466 TABLE is the table element to transcode. INFO is a plist used as
1467 a communication channel."
1468 (let ((widths (make-vector (cdr (org-export-table-dimensions table info)) 0)))
1469 (org-element-map table 'table-row
1470 (lambda (row)
1471 (let ((idx 0))
1472 (org-element-map row 'table-cell
1473 (lambda (cell)
1474 ;; Length of the cell in the original buffer is only an
1475 ;; approximation of the length of the cell in the
1476 ;; output. It can sometimes fail (e.g. it considers
1477 ;; "/a/" being larger than "ab").
1478 (let ((w (- (org-element-property :contents-end cell)
1479 (org-element-property :contents-begin cell))))
1480 (aset widths idx (max w (aref widths idx))))
1481 (cl-incf idx))
1482 info)))
1483 info)
1484 (format "{%s}" (mapconcat (lambda (w) (make-string w ?a)) widths "} {"))))
1486 ;;;; Table Cell
1488 (defun org-texinfo-table-cell (table-cell contents info)
1489 "Transcode a TABLE-CELL element from Org to Texinfo.
1490 CONTENTS is the cell contents. INFO is a plist used as
1491 a communication channel."
1492 (concat
1493 (let ((scientific-notation
1494 (plist-get info :texinfo-table-scientific-notation)))
1495 (if (and contents
1496 scientific-notation
1497 (string-match orgtbl-exp-regexp contents))
1498 ;; Use appropriate format string for scientific notation.
1499 (format scientific-notation
1500 (match-string 1 contents)
1501 (match-string 2 contents))
1502 contents))
1503 (when (org-export-get-next-element table-cell info) "\n@tab ")))
1505 ;;;; Table Row
1507 (defun org-texinfo-table-row (table-row contents info)
1508 "Transcode a TABLE-ROW element from Org to Texinfo.
1509 CONTENTS is the contents of the row. INFO is a plist used as
1510 a communication channel."
1511 ;; Rules are ignored since table separators are deduced from
1512 ;; borders of the current row.
1513 (when (eq (org-element-property :type table-row) 'standard)
1514 (let ((rowgroup-tag
1515 (if (and (= 1 (org-export-table-row-group table-row info))
1516 (org-export-table-has-header-p
1517 (org-export-get-parent-table table-row) info))
1518 "@headitem "
1519 "@item ")))
1520 (concat rowgroup-tag contents "\n"))))
1522 ;;;; Target
1524 (defun org-texinfo-target (target _contents info)
1525 "Transcode a TARGET object from Org to Texinfo.
1526 CONTENTS is nil. INFO is a plist holding contextual
1527 information."
1528 (format "@anchor{%s}" (org-texinfo--get-node target info)))
1530 ;;;; Timestamp
1532 (defun org-texinfo-timestamp (timestamp _contents info)
1533 "Transcode a TIMESTAMP object from Org to Texinfo.
1534 CONTENTS is nil. INFO is a plist holding contextual
1535 information."
1536 (let ((value (org-texinfo-plain-text
1537 (org-timestamp-translate timestamp) info)))
1538 (pcase (org-element-property :type timestamp)
1539 ((or `active `active-range)
1540 (format (plist-get info :texinfo-active-timestamp-format) value))
1541 ((or `inactive `inactive-range)
1542 (format (plist-get info :texinfo-inactive-timestamp-format) value))
1543 (_ (format (plist-get info :texinfo-diary-timestamp-format) value)))))
1545 ;;;; Underline
1547 (defun org-texinfo-underline (_underline contents info)
1548 "Transcode UNDERLINE from Org to Texinfo.
1549 CONTENTS is the text with underline markup. INFO is a plist
1550 holding contextual information."
1551 (org-texinfo--text-markup contents 'underline info))
1553 ;;;; Verbatim
1555 (defun org-texinfo-verbatim (verbatim _contents info)
1556 "Transcode a VERBATIM object from Org to Texinfo.
1557 CONTENTS is nil. INFO is a plist used as a communication
1558 channel."
1559 (org-texinfo--text-markup
1560 (org-element-property :value verbatim) 'verbatim info))
1562 ;;;; Verse Block
1564 (defun org-texinfo-verse-block (_verse-block contents _info)
1565 "Transcode a VERSE-BLOCK element from Org to Texinfo.
1566 CONTENTS is verse block contents. INFO is a plist holding
1567 contextual information."
1568 (format "@display\n%s@end display" contents))
1571 ;;; Interactive functions
1573 (defun org-texinfo-export-to-texinfo
1574 (&optional async subtreep visible-only body-only ext-plist)
1575 "Export current buffer to a Texinfo file.
1577 If narrowing is active in the current buffer, only export its
1578 narrowed part.
1580 If a region is active, export that region.
1582 A non-nil optional argument ASYNC means the process should happen
1583 asynchronously. The resulting file should be accessible through
1584 the `org-export-stack' interface.
1586 When optional argument SUBTREEP is non-nil, export the sub-tree
1587 at point, extracting information from the headline properties
1588 first.
1590 When optional argument VISIBLE-ONLY is non-nil, don't export
1591 contents of hidden elements.
1593 When optional argument BODY-ONLY is non-nil, only write code
1594 between \"\\begin{document}\" and \"\\end{document}\".
1596 EXT-PLIST, when provided, is a property list with external
1597 parameters overriding Org default settings, but still inferior to
1598 file-local settings.
1600 Return output file's name."
1601 (interactive)
1602 (let ((outfile (org-export-output-file-name ".texi" subtreep))
1603 (org-export-coding-system org-texinfo-coding-system))
1604 (org-export-to-file 'texinfo outfile
1605 async subtreep visible-only body-only ext-plist)))
1607 (defun org-texinfo-export-to-info
1608 (&optional async subtreep visible-only body-only ext-plist)
1609 "Export current buffer to Texinfo then process through to INFO.
1611 If narrowing is active in the current buffer, only export its
1612 narrowed part.
1614 If a region is active, export that region.
1616 A non-nil optional argument ASYNC means the process should happen
1617 asynchronously. The resulting file should be accessible through
1618 the `org-export-stack' interface.
1620 When optional argument SUBTREEP is non-nil, export the sub-tree
1621 at point, extracting information from the headline properties
1622 first.
1624 When optional argument VISIBLE-ONLY is non-nil, don't export
1625 contents of hidden elements.
1627 When optional argument BODY-ONLY is non-nil, only write code
1628 between \"\\begin{document}\" and \"\\end{document}\".
1630 EXT-PLIST, when provided, is a property list with external
1631 parameters overriding Org default settings, but still inferior to
1632 file-local settings.
1634 When optional argument PUB-DIR is set, use it as the publishing
1635 directory.
1637 Return INFO file's name."
1638 (interactive)
1639 (let ((outfile (org-export-output-file-name ".texi" subtreep))
1640 (org-export-coding-system org-texinfo-coding-system))
1641 (org-export-to-file 'texinfo outfile
1642 async subtreep visible-only body-only ext-plist
1643 (lambda (file) (org-texinfo-compile file)))))
1645 ;;;###autoload
1646 (defun org-texinfo-publish-to-texinfo (plist filename pub-dir)
1647 "Publish an org file to Texinfo.
1649 FILENAME is the filename of the Org file to be published. PLIST
1650 is the property list for the given project. PUB-DIR is the
1651 publishing directory.
1653 Return output file name."
1654 (org-publish-org-to 'texinfo filename ".texi" plist pub-dir))
1656 ;;;###autoload
1657 (defun org-texinfo-convert-region-to-texinfo ()
1658 "Assume the current region has Org syntax, and convert it to Texinfo.
1659 This can be used in any buffer. For example, you can write an
1660 itemized list in Org syntax in an Texinfo buffer and use this
1661 command to convert it."
1662 (interactive)
1663 (org-export-replace-region-by 'texinfo))
1665 (defun org-texinfo-compile (file)
1666 "Compile a texinfo file.
1668 FILE is the name of the file being compiled. Processing is done
1669 through the command specified in `org-texinfo-info-process',
1670 which see. Output is redirected to \"*Org INFO Texinfo Output*\"
1671 buffer.
1673 Return INFO file name or an error if it couldn't be produced."
1674 (message "Processing Texinfo file %s..." file)
1675 (let* ((log-name "*Org INFO Texinfo Output*")
1676 (log (get-buffer-create log-name))
1677 (output
1678 (org-compile-file file org-texinfo-info-process "info"
1679 (format "See %S for details" log-name)
1680 log)))
1681 (when org-texinfo-remove-logfiles
1682 (let ((base (file-name-sans-extension output)))
1683 (dolist (ext org-texinfo-logfiles-extensions)
1684 (let ((file (concat base "." ext)))
1685 (when (file-exists-p file) (delete-file file))))))
1686 (message "Process completed.")
1687 output))
1690 (provide 'ox-texinfo)
1692 ;; Local variables:
1693 ;; generated-autoload-file: "org-loaddefs.el"
1694 ;; End:
1696 ;;; ox-texinfo.el ends here