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