ox-texinfo: Move menu handling from sections to headlines
[org-mode.git] / lisp / ox-texinfo.el
blob5ea4af01b26eccfbb56201242fc07b764c0697b4
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" "@unnumbered %s" "@appendix %s")
150 ("@section %s" "@unnumberedsec %s" "@appendixsec %s")
151 ("@subsection %s" "@unnumberedsubsec %s" "@appendixsubsec %s")
152 ("@subsubsection %s" "@unnumberedsubsubsec %s" "@appendixsubsubsec %s")))
153 "Alist of Texinfo classes and associated header and structure.
154 If #+TEXINFO_CLASS is set in the buffer, use its value and the
155 associated information. Here is the structure of a class
156 definition:
158 (class-name
159 header-string
160 (numbered-1 unnumbered-1 appendix-1)
161 (numbered-2 unnumbered-2 appendix-2)
162 ...)
165 The header string
166 -----------------
168 The header string is inserted in the header of the generated
169 document, right after \"@setfilename\" and \"@settitle\"
170 commands.
172 If it contains the special string
174 \"@documentencoding AUTO\"
176 \"AUTO\" will be replaced with an appropriate coding system. See
177 `org-texinfo-coding-system' for more information. Likewise, if
178 the string contains the special string
180 \"@documentlanguage AUTO\"
182 \"AUTO\" will be replaced with the language defined in the
183 buffer, through #+LANGUAGE keyword, or globally, with
184 `org-export-default-language', which see.
187 The sectioning structure
188 ------------------------
190 The sectioning structure of the class is given by the elements
191 following the header string. For each sectioning level, a number
192 of strings is specified. A %s formatter is mandatory in each
193 section string and will be replaced by the title of the section."
194 :group 'org-export-texinfo
195 :version "26.1"
196 :package-version '(Org . "9.1")
197 :type '(repeat
198 (list (string :tag "Texinfo class")
199 (string :tag "Texinfo header")
200 (repeat :tag "Levels" :inline t
201 (choice
202 (list :tag "Heading"
203 (string :tag " numbered")
204 (string :tag "unnumbered")
205 (string :tag " appendix")))))))
207 ;;;; Headline
209 (defcustom org-texinfo-format-headline-function
210 'org-texinfo-format-headline-default-function
211 "Function to format headline text.
213 This function will be called with 5 arguments:
214 TODO the todo keyword (string or nil).
215 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
216 PRIORITY the priority of the headline (integer or nil)
217 TEXT the main headline text (string).
218 TAGS the tags as a list of strings (list of strings or nil).
220 The function result will be used in the section format string."
221 :group 'org-export-texinfo
222 :type 'function
223 :version "26.1"
224 :package-version '(Org . "8.3"))
226 ;;;; Node listing (menu)
228 (defcustom org-texinfo-node-description-column 32
229 "Column at which to start the description in the node listings.
230 If a node title is greater than this length, the description will
231 be placed after the end of the title."
232 :group 'org-export-texinfo
233 :type 'integer)
235 ;;;; Timestamps
237 (defcustom org-texinfo-active-timestamp-format "@emph{%s}"
238 "A printf format string to be applied to active timestamps."
239 :group 'org-export-texinfo
240 :type 'string)
242 (defcustom org-texinfo-inactive-timestamp-format "@emph{%s}"
243 "A printf format string to be applied to inactive timestamps."
244 :group 'org-export-texinfo
245 :type 'string)
247 (defcustom org-texinfo-diary-timestamp-format "@emph{%s}"
248 "A printf format string to be applied to diary timestamps."
249 :group 'org-export-texinfo
250 :type 'string)
252 ;;;; Links
254 (defcustom org-texinfo-link-with-unknown-path-format "@indicateurl{%s}"
255 "Format string for links with unknown path type."
256 :group 'org-export-texinfo
257 :type 'string)
259 ;;;; Tables
261 (defcustom org-texinfo-tables-verbatim nil
262 "When non-nil, tables are exported verbatim."
263 :group 'org-export-texinfo
264 :type 'boolean)
266 (defcustom org-texinfo-table-scientific-notation "%s\\,(%s)"
267 "Format string to display numbers in scientific notation.
269 The format should have \"%s\" twice, for mantissa and exponent
270 \(i.e. \"%s\\\\times10^{%s}\").
272 When nil, no transformation is made."
273 :group 'org-export-texinfo
274 :type '(choice
275 (string :tag "Format string")
276 (const :tag "No formatting" nil)))
278 (defcustom org-texinfo-table-default-markup "@asis"
279 "Default markup for first column in two-column tables.
281 This should an indicating command, e.g., \"@code\", \"@kbd\" or
282 \"@samp\".
284 It can be overridden locally using the \":indic\" attribute."
285 :group 'org-export-texinfo
286 :type 'string
287 :version "26.1"
288 :package-version '(Org . "9.1")
289 :safe #'stringp)
291 ;;;; Text markup
293 (defcustom org-texinfo-text-markup-alist '((bold . "@strong{%s}")
294 (code . code)
295 (italic . "@emph{%s}")
296 (verbatim . samp))
297 "Alist of Texinfo expressions to convert text markup.
299 The key must be a symbol among `bold', `code', `italic',
300 `strike-through', `underscore' and `verbatim'. The value is
301 a formatting string to wrap fontified text with.
303 Value can also be set to the following symbols: `verb', `samp'
304 and `code'. With the first one, Org uses \"@verb\" to create
305 a format string and selects a delimiter character that isn't in
306 the string. For the other two, Org uses \"@samp\" or \"@code\"
307 to typeset and protects special characters.
309 When no association is found for a given markup, text is returned
310 as-is."
311 :group 'org-export-texinfo
312 :version "26.1"
313 :package-version '(Org . "9.1")
314 :type 'alist
315 :options '(bold code italic strike-through underscore verbatim))
317 ;;;; Drawers
319 (defcustom org-texinfo-format-drawer-function (lambda (_name contents) contents)
320 "Function called to format a drawer in Texinfo code.
322 The function must accept two parameters:
323 NAME the drawer name, like \"LOGBOOK\"
324 CONTENTS the contents of the drawer.
326 The function should return the string to be exported.
328 The default function simply returns the value of CONTENTS."
329 :group 'org-export-texinfo
330 :version "24.4"
331 :package-version '(Org . "8.2")
332 :type 'function)
334 ;;;; Inlinetasks
336 (defcustom org-texinfo-format-inlinetask-function
337 'org-texinfo-format-inlinetask-default-function
338 "Function called to format an inlinetask in Texinfo code.
340 The function must accept six parameters:
341 TODO the todo keyword, as a string
342 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
343 PRIORITY the inlinetask priority, as a string
344 NAME the inlinetask name, as a string.
345 TAGS the inlinetask tags, as a list of strings.
346 CONTENTS the contents of the inlinetask, as a string.
348 The function should return the string to be exported."
349 :group 'org-export-texinfo
350 :type 'function)
352 ;;;; Compilation
354 (defcustom org-texinfo-info-process '("makeinfo %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 :type '(repeat :tag "Shell command sequence"
365 (string :tag "Shell command")))
367 (defcustom org-texinfo-logfiles-extensions
368 '("aux" "toc" "cp" "fn" "ky" "pg" "tp" "vr")
369 "The list of file extensions to consider as Texinfo logfiles.
370 The logfiles will be remove if `org-texinfo-remove-logfiles' is
371 non-nil."
372 :group 'org-export-texinfo
373 :type '(repeat (string :tag "Extension")))
375 (defcustom org-texinfo-remove-logfiles t
376 "Non-nil means remove the logfiles produced by compiling a Texinfo file.
377 By default, logfiles are files with these extensions: .aux, .toc,
378 .cp, .fn, .ky, .pg and .tp. To define the set of logfiles to remove,
379 set `org-texinfo-logfiles-extensions'."
380 :group 'org-export-latex
381 :type 'boolean)
383 ;;; Constants
385 (defconst org-texinfo-max-toc-depth 4
386 "Maximum depth for creation of detailed menu listings.
387 Beyond this depth, Texinfo will not recognize the nodes and will
388 cause errors. Left as a constant in case this value ever
389 changes.")
391 (defconst org-texinfo-supported-coding-systems
392 '("US-ASCII" "UTF-8" "ISO-8859-15" "ISO-8859-1" "ISO-8859-2" "koi8-r" "koi8-u")
393 "List of coding systems supported by Texinfo, as strings.
394 Specified coding system will be matched against these strings.
395 If two strings share the same prefix (e.g. \"ISO-8859-1\" and
396 \"ISO-8859-15\"), the most specific one has to be listed first.")
398 (defconst org-texinfo-inline-image-rules
399 (list (cons "file"
400 (regexp-opt '("eps" "pdf" "png" "jpg" "jpeg" "gif" "svg"))))
401 "Rules characterizing image files that can be inlined.")
404 ;;; Internal Functions
406 (defun org-texinfo--filter-section-blank-lines (headline _backend _info)
407 "Filter controlling number of blank lines after a section."
408 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" "\n\n" headline))
410 (defun org-texinfo--normalize-headlines (tree _backend info)
411 "Normalize headlines in TREE.
413 BACK-END is the symbol specifying back-end used for export. INFO
414 is a plist used as a communication channel.
416 Make sure every headline in TREE contains a section, since those
417 are required to install a menu. Also put exactly one blank line
418 at the end of each section.
420 Return new tree."
421 (org-element-map tree 'headline
422 (lambda (hl)
423 (org-element-put-property hl :post-blank 1)
424 (let ((contents (org-element-contents hl)))
425 (when contents
426 (let ((first (org-element-map contents '(headline section)
427 #'identity info t)))
428 (unless (eq (org-element-type first) 'section)
429 (apply #'org-element-set-contents
431 (cons `(section (:parent ,hl)) contents)))))))
432 info)
433 tree)
435 (defun org-texinfo--find-verb-separator (s)
436 "Return a character not used in string S.
437 This is used to choose a separator for constructs like \\verb."
438 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
439 (cl-loop for c across ll
440 when (not (string-match (regexp-quote (char-to-string c)) s))
441 return (char-to-string c))))
443 (defun org-texinfo--text-markup (text markup _info)
444 "Format TEXT depending on MARKUP text markup.
445 INFO is a plist used as a communication channel. See
446 `org-texinfo-text-markup-alist' for details."
447 (pcase (cdr (assq markup org-texinfo-text-markup-alist))
448 (`nil text) ;no markup: return raw text
449 (`code (format "@code{%s}" (org-texinfo--sanitize-content text)))
450 (`samp (format "@samp{%s}" (org-texinfo--sanitize-content text)))
451 (`verb
452 (let ((separator (org-texinfo--find-verb-separator text)))
453 (format "@verb{%s%s%s}" separator text separator)))
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 "@example\n%s@end example"
790 (org-texinfo--sanitize-content
791 (org-export-format-code-default example-block info))))
793 ;;; Export Block
795 (defun org-texinfo-export-block (export-block _contents _info)
796 "Transcode a EXPORT-BLOCK element from Org to Texinfo.
797 CONTENTS is nil. INFO is a plist holding contextual information."
798 (when (string= (org-element-property :type export-block) "TEXINFO")
799 (org-remove-indentation (org-element-property :value export-block))))
801 ;;; Export Snippet
803 (defun org-texinfo-export-snippet (export-snippet _contents _info)
804 "Transcode a EXPORT-SNIPPET object from Org to Texinfo.
805 CONTENTS is nil. INFO is a plist holding contextual information."
806 (when (eq (org-export-snippet-backend export-snippet) 'texinfo)
807 (org-element-property :value export-snippet)))
809 ;;;; Fixed Width
811 (defun org-texinfo-fixed-width (fixed-width _contents _info)
812 "Transcode a FIXED-WIDTH element from Org to Texinfo.
813 CONTENTS is nil. INFO is a plist holding contextual information."
814 (format "@example\n%s@end example"
815 (org-remove-indentation
816 (org-texinfo--sanitize-content
817 (org-element-property :value fixed-width)))))
819 ;;;; Footnote Reference
821 (defun org-texinfo-footnote-reference (footnote _contents info)
822 "Create a footnote reference for FOOTNOTE.
824 FOOTNOTE is the footnote to define. CONTENTS is nil. INFO is a
825 plist holding contextual information."
826 (let ((def (org-export-get-footnote-definition footnote info)))
827 (format "@footnote{%s}"
828 (org-trim (org-export-data def info)))))
830 ;;;; Headline
832 (defun org-texinfo--structuring-command (headline info)
833 "Return Texinfo structuring command string for HEADLINE element.
834 Return nil if HEADLINE is to be ignored, `plain-list' if it
835 should be exported as a plain-list item. INFO is a plist holding
836 contextual information."
837 (cond
838 ((org-element-property :footnote-section-p headline) nil)
839 ((org-not-nil (org-export-get-node-property :COPYING headline t)) nil)
840 ((org-export-low-level-p headline info) 'plain-list)
842 (let ((class (plist-get info :texinfo-class)))
843 (pcase (assoc class (plist-get info :texinfo-classes))
844 (`(,_ ,_ . ,sections)
845 (pcase (nth (1- (org-export-get-relative-level headline info))
846 sections)
847 (`(,numbered ,unnumbered ,appendix)
848 (cond
849 ((org-not-nil (org-export-get-node-property :APPENDIX headline t))
850 appendix)
851 ((org-not-nil (org-export-get-node-property :INDEX headline t))
852 unnumbered)
853 ((org-export-numbered-headline-p headline info) numbered)
854 (t unnumbered)))
855 (`nil 'plain-list)
856 (_ (user-error "Invalid Texinfo class specification: %S" class))))
857 (_ (user-error "Invalid Texinfo class specification: %S" class)))))))
859 (defun org-texinfo-headline (headline contents info)
860 "Transcode a HEADLINE element from Org to Texinfo.
861 CONTENTS holds the contents of the headline. INFO is a plist
862 holding contextual information."
863 (let ((section-fmt (org-texinfo--structuring-command headline info)))
864 (when section-fmt
865 (let* ((todo
866 (and (plist-get info :with-todo-keywords)
867 (let ((todo (org-element-property :todo-keyword headline)))
868 (and todo (org-export-data todo info)))))
869 (todo-type (and todo (org-element-property :todo-type headline)))
870 (tags (and (plist-get info :with-tags)
871 (org-export-get-tags headline info)))
872 (priority (and (plist-get info :with-priority)
873 (org-element-property :priority headline)))
874 (text (org-texinfo--sanitize-title
875 (org-element-property :title headline) info))
876 (full-text
877 (funcall (plist-get info :texinfo-format-headline-function)
878 todo todo-type priority text tags))
879 (contents
880 (concat (if (org-string-nw-p contents)
881 (concat "\n" contents)
883 (let ((index (org-element-property :INDEX headline)))
884 (and (member index '("cp" "fn" "ky" "pg" "tp" "vr"))
885 (format "\n@printindex %s\n" index))))))
886 (cond
887 ((eq section-fmt 'plain-list)
888 (let ((numbered? (org-export-numbered-headline-p headline info)))
889 (concat (and (org-export-first-sibling-p headline info)
890 (format "@%s\n" (if numbered? 'enumerate 'itemize)))
891 "@item\n" full-text "\n"
892 contents
893 (if (org-export-last-sibling-p headline info)
894 (format "@end %s" (if numbered? 'enumerate 'itemize))
895 "\n"))))
897 (concat (format "@node %s\n" (org-texinfo--get-node headline info))
898 (format section-fmt full-text)
899 contents "\n"
900 (org-texinfo-make-menu headline info))))))))
902 (defun org-texinfo-format-headline-default-function
903 (todo _todo-type priority text tags)
904 "Default format function for a headline.
905 See `org-texinfo-format-headline-function' for details."
906 (concat (when todo (format "@strong{%s} " todo))
907 (when priority (format "@emph{#%s} " priority))
908 text
909 (when tags (format " :%s:" (mapconcat 'identity tags ":")))))
911 ;;;; Inline Src Block
913 (defun org-texinfo-inline-src-block (inline-src-block _contents _info)
914 "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
915 CONTENTS holds the contents of the item. INFO is a plist holding
916 contextual information."
917 (format "@code{%s}"
918 (org-texinfo--sanitize-content
919 (org-element-property :value inline-src-block))))
921 ;;;; Inlinetask
923 (defun org-texinfo-inlinetask (inlinetask contents info)
924 "Transcode an INLINETASK element from Org to Texinfo.
925 CONTENTS holds the contents of the block. INFO is a plist
926 holding contextual information."
927 (let ((title (org-export-data (org-element-property :title inlinetask) info))
928 (todo (and (plist-get info :with-todo-keywords)
929 (let ((todo (org-element-property :todo-keyword inlinetask)))
930 (and todo (org-export-data todo info)))))
931 (todo-type (org-element-property :todo-type inlinetask))
932 (tags (and (plist-get info :with-tags)
933 (org-export-get-tags inlinetask info)))
934 (priority (and (plist-get info :with-priority)
935 (org-element-property :priority inlinetask))))
936 (funcall (plist-get info :texinfo-format-inlinetask-function)
937 todo todo-type priority title tags contents)))
939 (defun org-texinfo-format-inlinetask-default-function
940 (todo _todo-type priority title tags contents)
941 "Default format function for a inlinetasks.
942 See `org-texinfo-format-inlinetask-function' for details."
943 (let ((full-title
944 (concat (when todo (format "@strong{%s} " todo))
945 (when priority (format "#%c " priority))
946 title
947 (when tags (format ":%s:" (mapconcat #'identity tags ":"))))))
948 (format "@center %s\n\n%s\n" full-title contents)))
950 ;;;; Italic
952 (defun org-texinfo-italic (_italic contents info)
953 "Transcode ITALIC from Org to Texinfo.
954 CONTENTS is the text with italic markup. INFO is a plist holding
955 contextual information."
956 (org-texinfo--text-markup contents 'italic info))
958 ;;;; Item
960 (defun org-texinfo-item (item contents info)
961 "Transcode an ITEM element from Org to Texinfo.
962 CONTENTS holds the contents of the item. INFO is a plist holding
963 contextual information."
964 (let* ((tag (org-element-property :tag item))
965 (split (org-string-nw-p
966 (org-export-read-attribute :attr_texinfo
967 (org-element-property :parent item)
968 :sep)))
969 (items (and tag
970 (let ((tag (org-export-data tag info)))
971 (if split
972 (split-string tag (regexp-quote split) t "[ \t\n]+")
973 (list tag))))))
974 (format "%s\n%s"
975 (pcase items
976 (`nil "@item")
977 (`(,item) (concat "@item " item))
978 (`(,item . ,items)
979 (concat "@item " item "\n"
980 (mapconcat (lambda (i) (concat "@itemx " i))
981 items
982 "\n"))))
983 (or contents ""))))
985 ;;;; Keyword
987 (defun org-texinfo-keyword (keyword _contents info)
988 "Transcode a KEYWORD element from Org to Texinfo.
989 CONTENTS is nil. INFO is a plist holding contextual information."
990 (let ((value (org-element-property :value keyword)))
991 (pcase (org-element-property :key keyword)
992 ("TEXINFO" value)
993 ("CINDEX" (format "@cindex %s" value))
994 ("FINDEX" (format "@findex %s" value))
995 ("KINDEX" (format "@kindex %s" value))
996 ("PINDEX" (format "@pindex %s" value))
997 ("TINDEX" (format "@tindex %s" value))
998 ("VINDEX" (format "@vindex %s" value))
999 ("TOC"
1000 (cond ((string-match-p "\\<tables\\>" value)
1001 (concat "@listoffloats "
1002 (org-export-translate "Table" :utf-8 info)))
1003 ((string-match-p "\\<listings\\>" value)
1004 (concat "@listoffloats "
1005 (org-export-translate "Listing" :utf-8 info))))))))
1007 ;;;; Line Break
1009 (defun org-texinfo-line-break (_line-break _contents _info)
1010 "Transcode a LINE-BREAK object from Org to Texinfo.
1011 CONTENTS is nil. INFO is a plist holding contextual information."
1012 "@*\n")
1014 ;;;; Link
1016 (defun org-texinfo--@ref (datum description info)
1017 "Return @ref command for element or object DATUM.
1018 DESCRIPTION is the name of the section to print, as a string."
1019 (let ((node-name (org-texinfo--get-node datum info))
1020 ;; Sanitize DESCRIPTION for cross-reference use. In
1021 ;; particular, remove colons as they seem to cause (even
1022 ;; within @asis{...} to the Texinfo reader.
1023 (title (replace-regexp-in-string
1024 "[ \t]*:+" ""
1025 (replace-regexp-in-string "," "@comma{}" description))))
1026 (if (equal title node-name)
1027 (format "@ref{%s}" node-name)
1028 (format "@ref{%s, , %s}" node-name title))))
1030 (defun org-texinfo-link (link desc info)
1031 "Transcode a LINK object from Org to Texinfo.
1032 DESC is the description part of the link, or the empty string.
1033 INFO is a plist holding contextual information. See
1034 `org-export-data'."
1035 (let* ((type (org-element-property :type link))
1036 (raw-path (org-element-property :path link))
1037 ;; Ensure DESC really exists, or set it to nil.
1038 (desc (and (not (string= desc "")) desc))
1039 (path (cond
1040 ((member type '("http" "https" "ftp"))
1041 (concat type ":" raw-path))
1042 ((string= type "file") (org-export-file-uri raw-path))
1043 (t raw-path))))
1044 (cond
1045 ((org-export-custom-protocol-maybe link desc 'texinfo))
1046 ((org-export-inline-image-p link org-texinfo-inline-image-rules)
1047 (org-texinfo--inline-image link info))
1048 ((equal type "radio")
1049 (let ((destination (org-export-resolve-radio-link link info)))
1050 (if (not destination) desc
1051 (org-texinfo--@ref destination desc info))))
1052 ((member type '("custom-id" "id" "fuzzy"))
1053 (let ((destination
1054 (if (equal type "fuzzy")
1055 (org-export-resolve-fuzzy-link link info)
1056 (org-export-resolve-id-link link info))))
1057 (pcase (org-element-type destination)
1058 (`nil
1059 (format org-texinfo-link-with-unknown-path-format
1060 (org-texinfo--sanitize-content path)))
1061 ;; Id link points to an external file.
1062 (`plain-text
1063 (if desc (format "@uref{file://%s,%s}" destination desc)
1064 (format "@uref{file://%s}" destination)))
1065 ((or `headline
1066 ;; Targets within headlines cannot be turned into
1067 ;; @anchor{}, so we refer to the headline parent
1068 ;; directly.
1069 (and `target
1070 (guard (eq 'headline
1071 (org-element-type
1072 (org-element-property :parent destination))))))
1073 (let ((headline (org-element-lineage destination '(headline) t)))
1074 (org-texinfo--@ref
1075 headline
1076 (or desc (org-texinfo--sanitize-title
1077 (org-element-property :title headline) info))
1078 info)))
1080 (org-texinfo--@ref
1081 destination
1082 (or desc
1083 (pcase (org-export-get-ordinal destination info)
1084 ((and (pred integerp) n) (number-to-string n))
1085 ((and (pred consp) n) (mapconcat #'number-to-string n "."))
1086 (_ "???"))) ;cannot guess the description
1087 info)))))
1088 ((string= type "mailto")
1089 (format "@email{%s}"
1090 (concat (org-texinfo--sanitize-content path)
1091 (and desc (concat ", " desc)))))
1092 ;; External link with a description part.
1093 ((and path desc) (format "@uref{%s, %s}" path desc))
1094 ;; External link without a description part.
1095 (path (format "@uref{%s}" path))
1096 ;; No path, only description. Try to do something useful.
1098 (format (plist-get info :texinfo-link-with-unknown-path-format) desc)))))
1100 (defun org-texinfo--inline-image (link info)
1101 "Return Texinfo code for an inline image.
1102 LINK is the link pointing to the inline image. INFO is the
1103 current state of the export, as a plist."
1104 (let* ((parent (org-export-get-parent-element link))
1105 (label (and (org-element-property :name parent)
1106 (org-texinfo--get-node parent info)))
1107 (caption (org-export-get-caption parent))
1108 (shortcaption (org-export-get-caption parent t))
1109 (path (org-element-property :path link))
1110 (filename
1111 (file-name-sans-extension
1112 (if (file-name-absolute-p path) (expand-file-name path) path)))
1113 (extension (file-name-extension path))
1114 (attributes (org-export-read-attribute :attr_texinfo parent))
1115 (height (or (plist-get attributes :height) ""))
1116 (width (or (plist-get attributes :width) ""))
1117 (alt (or (plist-get attributes :alt) ""))
1118 (image (format "@image{%s,%s,%s,%s,%s}"
1119 filename width height alt extension)))
1120 (cond ((or caption shortcaption)
1121 (org-texinfo--wrap-float image
1122 info
1123 (org-export-translate "Figure" :utf-8 info)
1124 label
1125 caption
1126 shortcaption))
1127 (label (concat "@anchor{" label "}\n" image))
1128 (t image))))
1131 ;;;; Menu
1133 (defun org-texinfo-make-menu (scope info &optional master)
1134 "Create the menu for inclusion in the Texinfo document.
1136 SCOPE is a headline or a full parse tree. INFO is the
1137 communication channel, as a plist.
1139 When optional argument MASTER is non-nil, generate a master menu,
1140 including detailed node listing."
1141 (let ((menu (org-texinfo--build-menu scope info)))
1142 (when (org-string-nw-p menu)
1143 (org-element-normalize-string
1144 (format
1145 "@menu\n%s@end menu"
1146 (concat menu
1147 (when master
1148 (let ((detailmenu
1149 (org-texinfo--build-menu
1150 scope info
1151 (let ((toc-depth (plist-get info :with-toc)))
1152 (if (wholenump toc-depth) toc-depth
1153 org-texinfo-max-toc-depth)))))
1154 (when (org-string-nw-p detailmenu)
1155 (concat "\n@detailmenu\n"
1156 "--- The Detailed Node Listing ---\n\n"
1157 detailmenu
1158 "@end detailmenu\n"))))))))))
1160 (defun org-texinfo--build-menu (scope info &optional level)
1161 "Build menu for entries within SCOPE.
1162 SCOPE is a headline or a full parse tree. INFO is a plist
1163 containing contextual information. When optional argument LEVEL
1164 is an integer, build the menu recursively, down to this depth."
1165 (cond
1166 ((not level)
1167 (org-texinfo--format-entries (org-texinfo--menu-entries scope info) info))
1168 ((zerop level) nil)
1170 (org-element-normalize-string
1171 (mapconcat
1172 (lambda (h)
1173 (let ((entries (org-texinfo--menu-entries h info)))
1174 (when entries
1175 (concat
1176 (format "%s\n\n%s\n"
1177 (org-export-data (org-export-get-alt-title h info) info)
1178 (org-texinfo--format-entries entries info))
1179 (org-texinfo--build-menu h info (1- level))))))
1180 (org-texinfo--menu-entries scope info) "\n")))))
1182 (defun org-texinfo--format-entries (entries info)
1183 "Format all direct menu entries in SCOPE, as a string.
1184 SCOPE is either a headline or a full Org document. INFO is
1185 a plist containing contextual information."
1186 (org-element-normalize-string
1187 (mapconcat
1188 (lambda (h)
1189 (let* ((title
1190 ;; Colons are used as a separator between title and node
1191 ;; name. Remove them.
1192 (replace-regexp-in-string
1193 "[ \t]+:+" ""
1194 (org-texinfo--sanitize-title
1195 (org-export-get-alt-title h info) info)))
1196 (node (org-texinfo--get-node h info))
1197 (entry (concat "* " title ":"
1198 (if (string= title node) ":"
1199 (concat " " node ". "))))
1200 (desc (org-element-property :DESCRIPTION h)))
1201 (if (not desc) entry
1202 (format (format "%%-%ds %%s" org-texinfo-node-description-column)
1203 entry desc))))
1204 entries "\n")))
1206 (defun org-texinfo--menu-entries (scope info)
1207 "List direct children in SCOPE needing a menu entry.
1208 SCOPE is a headline or a full parse tree. INFO is a plist
1209 holding contextual information."
1210 (let* ((cache (or (plist-get info :texinfo-entries-cache)
1211 (plist-get (plist-put info :texinfo-entries-cache
1212 (make-hash-table :test #'eq))
1213 :texinfo-entries-cache)))
1214 (cached-entries (gethash scope cache 'no-cache)))
1215 (if (not (eq cached-entries 'no-cache)) cached-entries
1216 (puthash scope
1217 (org-element-map (org-element-contents scope) 'headline
1218 (lambda (h)
1219 (and (not (org-not-nil (org-element-property :COPYING h)))
1220 (not (org-element-property :footnote-section-p h))
1221 (not (org-export-low-level-p h info))
1223 info nil 'headline)
1224 cache))))
1226 ;;;; Node Property
1228 (defun org-texinfo-node-property (node-property _contents _info)
1229 "Transcode a NODE-PROPERTY element from Org to Texinfo.
1230 CONTENTS is nil. INFO is a plist holding contextual
1231 information."
1232 (format "%s:%s"
1233 (org-element-property :key node-property)
1234 (let ((value (org-element-property :value node-property)))
1235 (if value (concat " " value) ""))))
1237 ;;;; Paragraph
1239 (defun org-texinfo-paragraph (_paragraph contents _info)
1240 "Transcode a PARAGRAPH element from Org to Texinfo.
1241 CONTENTS is the contents of the paragraph, as a string. INFO is
1242 the plist used as a communication channel."
1243 contents)
1245 ;;;; Plain List
1247 (defun org-texinfo-plain-list (plain-list contents info)
1248 "Transcode a PLAIN-LIST element from Org to Texinfo.
1249 CONTENTS is the contents of the list. INFO is a plist holding
1250 contextual information."
1251 (let* ((attr (org-export-read-attribute :attr_texinfo plain-list))
1252 (indic (let ((i (or (plist-get attr :indic)
1253 (plist-get info :texinfo-table-default-markup))))
1254 ;; Allow indicating commands with missing @ sign.
1255 (if (string-prefix-p "@" i) i (concat "@" i))))
1256 (table-type (plist-get attr :table-type))
1257 (type (org-element-property :type plain-list))
1258 (list-type (cond
1259 ((eq type 'ordered) "enumerate")
1260 ((eq type 'unordered) "itemize")
1261 ((member table-type '("ftable" "vtable")) table-type)
1262 (t "table"))))
1263 (format "@%s\n%s@end %s"
1264 (if (eq type 'descriptive) (concat list-type " " indic) list-type)
1265 contents
1266 list-type)))
1268 ;;;; Plain Text
1270 (defun org-texinfo-plain-text (text info)
1271 "Transcode a TEXT string from Org to Texinfo.
1272 TEXT is the string to transcode. INFO is a plist holding
1273 contextual information."
1274 ;; First protect @, { and }.
1275 (let ((output (org-texinfo--sanitize-content text)))
1276 ;; Activate smart quotes. Be sure to provide original TEXT string
1277 ;; since OUTPUT may have been modified.
1278 (when (plist-get info :with-smart-quotes)
1279 (setq output
1280 (org-export-activate-smart-quotes output :texinfo info text)))
1281 ;; LaTeX into @LaTeX{} and TeX into @TeX{}
1282 (let ((case-fold-search nil))
1283 (setq output (replace-regexp-in-string "\\(?:La\\)?TeX" "@\\&{}" output)))
1284 ;; Convert special strings.
1285 (when (plist-get info :with-special-strings)
1286 (setq output
1287 (replace-regexp-in-string
1288 "\\.\\.\\." "@dots{}"
1289 (replace-regexp-in-string "\\\\-" "@-" output))))
1290 ;; Handle break preservation if required.
1291 (when (plist-get info :preserve-breaks)
1292 (setq output (replace-regexp-in-string
1293 "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" output)))
1294 ;; Return value.
1295 output))
1297 ;;;; Planning
1299 (defun org-texinfo-planning (planning _contents info)
1300 "Transcode a PLANNING element from Org to Texinfo.
1301 CONTENTS is nil. INFO is a plist holding contextual
1302 information."
1303 (concat
1304 "@noindent"
1305 (mapconcat
1306 'identity
1307 (delq nil
1308 (list
1309 (let ((closed (org-element-property :closed planning)))
1310 (when closed
1311 (concat
1312 (format "@strong{%s} " org-closed-string)
1313 (format (plist-get info :texinfo-inactive-timestamp-format)
1314 (org-timestamp-translate closed)))))
1315 (let ((deadline (org-element-property :deadline planning)))
1316 (when deadline
1317 (concat
1318 (format "@strong{%s} " org-deadline-string)
1319 (format (plist-get info :texinfo-active-timestamp-format)
1320 (org-timestamp-translate deadline)))))
1321 (let ((scheduled (org-element-property :scheduled planning)))
1322 (when scheduled
1323 (concat
1324 (format "@strong{%s} " org-scheduled-string)
1325 (format (plist-get info :texinfo-active-timestamp-format)
1326 (org-timestamp-translate scheduled)))))))
1327 " ")
1328 "@*"))
1330 ;;;; Property Drawer
1332 (defun org-texinfo-property-drawer (_property-drawer contents _info)
1333 "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
1334 CONTENTS holds the contents of the drawer. INFO is a plist
1335 holding contextual information."
1336 (and (org-string-nw-p contents)
1337 (format "@verbatim\n%s@end verbatim" contents)))
1339 ;;;; Quote Block
1341 (defun org-texinfo-quote-block (quote-block contents _info)
1342 "Transcode a QUOTE-BLOCK element from Org to Texinfo.
1343 CONTENTS holds the contents of the block. INFO is a plist
1344 holding contextual information."
1345 (let* ((title (org-element-property :name quote-block))
1346 (start-quote (concat "@quotation"
1347 (if title
1348 (format " %s" title)))))
1349 (format "%s\n%s@end quotation" start-quote contents)))
1351 ;;;; Radio Target
1353 (defun org-texinfo-radio-target (radio-target text info)
1354 "Transcode a RADIO-TARGET object from Org to Texinfo.
1355 TEXT is the text of the target. INFO is a plist holding
1356 contextual information."
1357 (format "@anchor{%s}%s"
1358 (org-texinfo--get-node radio-target info)
1359 text))
1361 ;;;; Section
1363 (defun org-texinfo-section (section contents _info)
1364 "Transcode a SECTION element from Org to Texinfo.
1365 CONTENTS holds the contents of the section."
1366 (and (org-export-get-parent-headline section) ;ignore first section
1367 contents))
1369 ;;;; Special Block
1371 (defun org-texinfo-special-block (special-block contents _info)
1372 "Transcode a SPECIAL-BLOCK element from Org to Texinfo.
1373 CONTENTS holds the contents of the block. INFO is a plist used
1374 as a communication channel."
1375 (let ((opt (org-export-read-attribute :attr_texinfo special-block :options))
1376 (type (org-element-property :type special-block)))
1377 (format "@%s%s\n%s@end %s"
1378 type
1379 (if opt (concat " " opt) "")
1380 (or contents "")
1381 type)))
1383 ;;;; Src Block
1385 (defun org-texinfo-src-block (src-block _contents info)
1386 "Transcode a SRC-BLOCK element from Org to Texinfo.
1387 CONTENTS holds the contents of the item. INFO is a plist holding
1388 contextual information."
1389 (let* ((lisp (string-match-p "lisp"
1390 (org-element-property :language src-block)))
1391 (code (org-texinfo--sanitize-content
1392 (org-export-format-code-default src-block info)))
1393 (value (format
1394 (if lisp "@lisp\n%s@end lisp" "@example\n%s@end example")
1395 code))
1396 (caption (org-export-get-caption src-block))
1397 (shortcaption (org-export-get-caption src-block t)))
1398 (if (not (or caption shortcaption)) value
1399 (org-texinfo--wrap-float value
1400 info
1401 (org-export-translate "Listing" :utf-8 info)
1402 (org-texinfo--get-node src-block info)
1403 caption
1404 shortcaption))))
1406 ;;;; Statistics Cookie
1408 (defun org-texinfo-statistics-cookie (statistics-cookie _contents _info)
1409 "Transcode a STATISTICS-COOKIE object from Org to Texinfo.
1410 CONTENTS is nil. INFO is a plist holding contextual information."
1411 (org-element-property :value statistics-cookie))
1414 ;;;; Strike-through
1416 (defun org-texinfo-strike-through (_strike-through contents info)
1417 "Transcode STRIKE-THROUGH from Org to Texinfo.
1418 CONTENTS is the text with strike-through markup. INFO is a plist
1419 holding contextual information."
1420 (org-texinfo--text-markup contents 'strike-through info))
1422 ;;;; Subscript
1424 (defun org-texinfo-subscript (_subscript contents _info)
1425 "Transcode a SUBSCRIPT object from Org to Texinfo.
1426 CONTENTS is the contents of the object. INFO is a plist holding
1427 contextual information."
1428 (format "@math{_%s}" contents))
1430 ;;;; Superscript
1432 (defun org-texinfo-superscript (_superscript contents _info)
1433 "Transcode a SUPERSCRIPT object from Org to Texinfo.
1434 CONTENTS is the contents of the object. INFO is a plist holding
1435 contextual information."
1436 (format "@math{^%s}" contents))
1438 ;;;; Table
1440 (defun org-texinfo-table (table contents info)
1441 "Transcode a TABLE element from Org to Texinfo.
1442 CONTENTS is the contents of the table. INFO is a plist holding
1443 contextual information."
1444 (if (eq (org-element-property :type table) 'table.el)
1445 (format "@verbatim\n%s@end verbatim"
1446 (org-element-normalize-string
1447 (org-element-property :value table)))
1448 (let* ((col-width (org-export-read-attribute :attr_texinfo table :columns))
1449 (columns
1450 (if col-width (format "@columnfractions %s" col-width)
1451 (org-texinfo-table-column-widths table info)))
1452 (caption (org-export-get-caption table))
1453 (shortcaption (org-export-get-caption table t))
1454 (table-str (format "@multitable %s\n%s@end multitable"
1455 columns
1456 contents)))
1457 (if (not (or caption shortcaption)) table-str
1458 (org-texinfo--wrap-float table-str
1459 info
1460 (org-export-translate "Table" :utf-8 info)
1461 (org-texinfo--get-node table info)
1462 caption
1463 shortcaption)))))
1465 (defun org-texinfo-table-column-widths (table info)
1466 "Determine the largest table cell in each column to process alignment.
1467 TABLE is the table element to transcode. INFO is a plist used as
1468 a communication channel."
1469 (let ((widths (make-vector (cdr (org-export-table-dimensions table info)) 0)))
1470 (org-element-map table 'table-row
1471 (lambda (row)
1472 (let ((idx 0))
1473 (org-element-map row 'table-cell
1474 (lambda (cell)
1475 ;; Length of the cell in the original buffer is only an
1476 ;; approximation of the length of the cell in the
1477 ;; output. It can sometimes fail (e.g. it considers
1478 ;; "/a/" being larger than "ab").
1479 (let ((w (- (org-element-property :contents-end cell)
1480 (org-element-property :contents-begin cell))))
1481 (aset widths idx (max w (aref widths idx))))
1482 (cl-incf idx))
1483 info)))
1484 info)
1485 (format "{%s}" (mapconcat (lambda (w) (make-string w ?a)) widths "} {"))))
1487 ;;;; Table Cell
1489 (defun org-texinfo-table-cell (table-cell contents info)
1490 "Transcode a TABLE-CELL element from Org to Texinfo.
1491 CONTENTS is the cell contents. INFO is a plist used as
1492 a communication channel."
1493 (concat
1494 (let ((scientific-notation
1495 (plist-get info :texinfo-table-scientific-notation)))
1496 (if (and contents
1497 scientific-notation
1498 (string-match orgtbl-exp-regexp contents))
1499 ;; Use appropriate format string for scientific notation.
1500 (format scientific-notation
1501 (match-string 1 contents)
1502 (match-string 2 contents))
1503 contents))
1504 (when (org-export-get-next-element table-cell info) "\n@tab ")))
1506 ;;;; Table Row
1508 (defun org-texinfo-table-row (table-row contents info)
1509 "Transcode a TABLE-ROW element from Org to Texinfo.
1510 CONTENTS is the contents of the row. INFO is a plist used as
1511 a communication channel."
1512 ;; Rules are ignored since table separators are deduced from
1513 ;; borders of the current row.
1514 (when (eq (org-element-property :type table-row) 'standard)
1515 (let ((rowgroup-tag
1516 (if (and (= 1 (org-export-table-row-group table-row info))
1517 (org-export-table-has-header-p
1518 (org-export-get-parent-table table-row) info))
1519 "@headitem "
1520 "@item ")))
1521 (concat rowgroup-tag contents "\n"))))
1523 ;;;; Target
1525 (defun org-texinfo-target (target _contents info)
1526 "Transcode a TARGET object from Org to Texinfo.
1527 CONTENTS is nil. INFO is a plist holding contextual
1528 information."
1529 (format "@anchor{%s}" (org-texinfo--get-node target info)))
1531 ;;;; Timestamp
1533 (defun org-texinfo-timestamp (timestamp _contents info)
1534 "Transcode a TIMESTAMP object from Org to Texinfo.
1535 CONTENTS is nil. INFO is a plist holding contextual
1536 information."
1537 (let ((value (org-texinfo-plain-text
1538 (org-timestamp-translate timestamp) info)))
1539 (pcase (org-element-property :type timestamp)
1540 ((or `active `active-range)
1541 (format (plist-get info :texinfo-active-timestamp-format) value))
1542 ((or `inactive `inactive-range)
1543 (format (plist-get info :texinfo-inactive-timestamp-format) value))
1544 (_ (format (plist-get info :texinfo-diary-timestamp-format) value)))))
1546 ;;;; Underline
1548 (defun org-texinfo-underline (_underline contents info)
1549 "Transcode UNDERLINE from Org to Texinfo.
1550 CONTENTS is the text with underline markup. INFO is a plist
1551 holding contextual information."
1552 (org-texinfo--text-markup contents 'underline info))
1554 ;;;; Verbatim
1556 (defun org-texinfo-verbatim (verbatim _contents info)
1557 "Transcode a VERBATIM object from Org to Texinfo.
1558 CONTENTS is nil. INFO is a plist used as a communication
1559 channel."
1560 (org-texinfo--text-markup
1561 (org-element-property :value verbatim) 'verbatim info))
1563 ;;;; Verse Block
1565 (defun org-texinfo-verse-block (_verse-block contents _info)
1566 "Transcode a VERSE-BLOCK element from Org to Texinfo.
1567 CONTENTS is verse block contents. INFO is a plist holding
1568 contextual information."
1569 (format "@display\n%s@end display" contents))
1572 ;;; Interactive functions
1574 (defun org-texinfo-export-to-texinfo
1575 (&optional async subtreep visible-only body-only ext-plist)
1576 "Export current buffer to a Texinfo file.
1578 If narrowing is active in the current buffer, only export its
1579 narrowed part.
1581 If a region is active, export that region.
1583 A non-nil optional argument ASYNC means the process should happen
1584 asynchronously. The resulting file should be accessible through
1585 the `org-export-stack' interface.
1587 When optional argument SUBTREEP is non-nil, export the sub-tree
1588 at point, extracting information from the headline properties
1589 first.
1591 When optional argument VISIBLE-ONLY is non-nil, don't export
1592 contents of hidden elements.
1594 When optional argument BODY-ONLY is non-nil, only write code
1595 between \"\\begin{document}\" and \"\\end{document}\".
1597 EXT-PLIST, when provided, is a property list with external
1598 parameters overriding Org default settings, but still inferior to
1599 file-local settings.
1601 Return output file's name."
1602 (interactive)
1603 (let ((outfile (org-export-output-file-name ".texi" subtreep))
1604 (org-export-coding-system org-texinfo-coding-system))
1605 (org-export-to-file 'texinfo outfile
1606 async subtreep visible-only body-only ext-plist)))
1608 (defun org-texinfo-export-to-info
1609 (&optional async subtreep visible-only body-only ext-plist)
1610 "Export current buffer to Texinfo then process through to INFO.
1612 If narrowing is active in the current buffer, only export its
1613 narrowed part.
1615 If a region is active, export that region.
1617 A non-nil optional argument ASYNC means the process should happen
1618 asynchronously. The resulting file should be accessible through
1619 the `org-export-stack' interface.
1621 When optional argument SUBTREEP is non-nil, export the sub-tree
1622 at point, extracting information from the headline properties
1623 first.
1625 When optional argument VISIBLE-ONLY is non-nil, don't export
1626 contents of hidden elements.
1628 When optional argument BODY-ONLY is non-nil, only write code
1629 between \"\\begin{document}\" and \"\\end{document}\".
1631 EXT-PLIST, when provided, is a property list with external
1632 parameters overriding Org default settings, but still inferior to
1633 file-local settings.
1635 When optional argument PUB-DIR is set, use it as the publishing
1636 directory.
1638 Return INFO file's name."
1639 (interactive)
1640 (let ((outfile (org-export-output-file-name ".texi" subtreep))
1641 (org-export-coding-system org-texinfo-coding-system))
1642 (org-export-to-file 'texinfo outfile
1643 async subtreep visible-only body-only ext-plist
1644 (lambda (file) (org-texinfo-compile file)))))
1646 ;;;###autoload
1647 (defun org-texinfo-publish-to-texinfo (plist filename pub-dir)
1648 "Publish an org file to Texinfo.
1650 FILENAME is the filename of the Org file to be published. PLIST
1651 is the property list for the given project. PUB-DIR is the
1652 publishing directory.
1654 Return output file name."
1655 (org-publish-org-to 'texinfo filename ".texi" plist pub-dir))
1657 ;;;###autoload
1658 (defun org-texinfo-convert-region-to-texinfo ()
1659 "Assume the current region has Org syntax, and convert it to Texinfo.
1660 This can be used in any buffer. For example, you can write an
1661 itemized list in Org syntax in an Texinfo buffer and use this
1662 command to convert it."
1663 (interactive)
1664 (org-export-replace-region-by 'texinfo))
1666 (defun org-texinfo-compile (file)
1667 "Compile a texinfo file.
1669 FILE is the name of the file being compiled. Processing is done
1670 through the command specified in `org-texinfo-info-process',
1671 which see. Output is redirected to \"*Org INFO Texinfo Output*\"
1672 buffer.
1674 Return INFO file name or an error if it couldn't be produced."
1675 (message "Processing Texinfo file %s..." file)
1676 (let* ((log-name "*Org INFO Texinfo Output*")
1677 (log (get-buffer-create log-name))
1678 (output
1679 (org-compile-file file org-texinfo-info-process "info"
1680 (format "See %S for details" log-name)
1681 log)))
1682 (when org-texinfo-remove-logfiles
1683 (let ((base (file-name-sans-extension output)))
1684 (dolist (ext org-texinfo-logfiles-extensions)
1685 (let ((file (concat base "." ext)))
1686 (when (file-exists-p file) (delete-file file))))))
1687 (message "Process completed.")
1688 output))
1691 (provide 'ox-texinfo)
1693 ;; Local variables:
1694 ;; generated-autoload-file: "org-loaddefs.el"
1695 ;; End:
1697 ;;; ox-texinfo.el ends here