Merge branch 'maint'
[org-mode.git] / lisp / ox-texinfo.el
blobcaec286fd9ac5b92d7320d4d96b10fa0bebfe2f6
1 ;;; ox-texinfo.el --- Texinfo Back-End for Org Export Engine -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2012-2015 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 (eval-when-compile (require 'cl))
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 (subscript . org-texinfo-subscript)
72 (superscript . org-texinfo-superscript)
73 (table . org-texinfo-table)
74 (table-cell . org-texinfo-table-cell)
75 (table-row . org-texinfo-table-row)
76 (target . org-texinfo-target)
77 (template . org-texinfo-template)
78 (timestamp . org-texinfo-timestamp)
79 (verbatim . org-texinfo-verbatim)
80 (verse-block . org-texinfo-verse-block))
81 :export-block "TEXINFO"
82 :filters-alist
83 '((:filter-headline . org-texinfo--filter-section-blank-lines)
84 (:filter-parse-tree . org-texinfo--normalize-headlines)
85 (:filter-section . org-texinfo--filter-section-blank-lines))
86 :menu-entry
87 '(?i "Export to Texinfo"
88 ((?t "As TEXI file" org-texinfo-export-to-texinfo)
89 (?i "As INFO file" org-texinfo-export-to-info)
90 (?o "As INFO file and open"
91 (lambda (a s v b)
92 (if a (org-texinfo-export-to-info t s v b)
93 (org-open-file (org-texinfo-export-to-info nil s v b)))))))
94 :options-alist
95 '((:texinfo-filename "TEXINFO_FILENAME" nil nil t)
96 (:texinfo-class "TEXINFO_CLASS" nil org-texinfo-default-class t)
97 (:texinfo-header "TEXINFO_HEADER" nil nil newline)
98 (:texinfo-post-header "TEXINFO_POST_HEADER" nil nil newline)
99 (:subtitle "SUBTITLE" nil nil parse)
100 (:subauthor "SUBAUTHOR" nil nil newline)
101 (:texinfo-dircat "TEXINFO_DIR_CATEGORY" nil nil t)
102 (:texinfo-dirtitle "TEXINFO_DIR_TITLE" nil nil t)
103 (:texinfo-dirdesc "TEXINFO_DIR_DESC" nil nil t)
104 (:texinfo-printed-title "TEXINFO_PRINTED_TITLE" nil nil t)
105 ;; Other variables.
106 (:texinfo-classes nil nil org-texinfo-classes)
107 (:texinfo-format-headline-function nil nil org-texinfo-format-headline-function)
108 (:texinfo-node-description-column nil nil org-texinfo-node-description-column)
109 (:texinfo-active-timestamp-format nil nil org-texinfo-active-timestamp-format)
110 (:texinfo-inactive-timestamp-format nil nil org-texinfo-inactive-timestamp-format)
111 (:texinfo-diary-timestamp-format nil nil org-texinfo-diary-timestamp-format)
112 (:texinfo-link-with-unknown-path-format nil nil org-texinfo-link-with-unknown-path-format)
113 (:texinfo-tables-verbatim nil nil org-texinfo-tables-verbatim)
114 (:texinfo-table-scientific-notation nil nil org-texinfo-table-scientific-notation)
115 (:texinfo-def-table-markup nil nil org-texinfo-def-table-markup)
116 (:texinfo-text-markup-alist nil nil org-texinfo-text-markup-alist)
117 (:texinfo-format-drawer-function nil nil org-texinfo-format-drawer-function)
118 (:texinfo-format-inlinetask-function nil nil org-texinfo-format-inlinetask-function)))
122 ;;; User Configurable Variables
124 (defgroup org-export-texinfo nil
125 "Options for exporting Org mode files to Texinfo."
126 :tag "Org Export Texinfo"
127 :version "24.4"
128 :package-version '(Org . "8.0")
129 :group 'org-export)
131 ;;;; Preamble
133 (defcustom org-texinfo-coding-system nil
134 "Default document encoding for Texinfo output.
136 If nil it will default to `buffer-file-coding-system'."
137 :group 'org-export-texinfo
138 :type 'coding-system)
140 (defcustom org-texinfo-default-class "info"
141 "The default Texinfo class."
142 :group 'org-export-texinfo
143 :type '(string :tag "Texinfo class"))
145 (defcustom org-texinfo-classes
146 '(("info"
147 "@documentencoding AUTO\n@documentlanguage AUTO"
148 ("@chapter %s" . "@unnumbered %s")
149 ("@section %s" . "@unnumberedsec %s")
150 ("@subsection %s" . "@unnumberedsubsec %s")
151 ("@subsubsection %s" . "@unnumberedsubsubsec %s")))
152 "Alist of Texinfo classes and associated header and structure.
153 If #+TEXINFO_CLASS is set in the buffer, use its value and the
154 associated information. Here is the structure of each cell:
156 (class-name
157 header-string
158 (numbered-section . unnumbered-section)
159 ...)
162 The header string
163 -----------------
165 The header string is inserted in the header of the generated
166 document, right after \"@setfilename\" and \"@settitle\"
167 commands.
169 If it contains the special string
171 \"@documentencoding AUTO\"
173 \"AUTO\" will be replaced with an appropriate coding system. See
174 `org-texinfo-coding-system' for more information. Likewise, if
175 the string contains the special string
177 \"@documentlanguage AUTO\"
179 \"AUTO\" will be replaced with the language defined in the
180 buffer, through #+LANGUAGE keyword, or globally, with
181 `org-export-default-language', which see.
184 The sectioning structure
185 ------------------------
187 The sectioning structure of the class is given by the elements
188 following the header string. For each sectioning level, a number
189 of strings is specified. A %s formatter is mandatory in each
190 section string and will be replaced by the title of the section.
192 Instead of a list of sectioning commands, you can also specify
193 a function name. That function will be called with two
194 parameters, the reduced) level of the headline, and a predicate
195 non-nil when the headline should be numbered. It must return
196 a format string in which the section title will be added."
197 :group 'org-export-texinfo
198 :version "24.4"
199 :package-version '(Org . "8.2")
200 :type '(repeat
201 (list (string :tag "Texinfo class")
202 (string :tag "Texinfo header")
203 (repeat :tag "Levels" :inline t
204 (choice
205 (cons :tag "Heading"
206 (string :tag " numbered")
207 (string :tag "unnumbered"))
208 (function :tag "Hook computing sectioning"))))))
210 ;;;; Headline
212 (defcustom org-texinfo-format-headline-function
213 'org-texinfo-format-headline-default-function
214 "Function to format headline text.
216 This function will be called with 5 arguments:
217 TODO the todo keyword (string or nil).
218 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
219 PRIORITY the priority of the headline (integer or nil)
220 TEXT the main headline text (string).
221 TAGS the tags as a list of strings (list of strings or nil).
223 The function result will be used in the section format string."
224 :group 'org-export-texinfo
225 :type 'function
226 :version "25.1"
227 :package-version '(Org . "8.3"))
229 ;;;; Node listing (menu)
231 (defcustom org-texinfo-node-description-column 32
232 "Column at which to start the description in the node listings.
233 If a node title is greater than this length, the description will
234 be placed after the end of the title."
235 :group 'org-export-texinfo
236 :type 'integer)
238 ;;;; Timestamps
240 (defcustom org-texinfo-active-timestamp-format "@emph{%s}"
241 "A printf format string to be applied to active timestamps."
242 :group 'org-export-texinfo
243 :type 'string)
245 (defcustom org-texinfo-inactive-timestamp-format "@emph{%s}"
246 "A printf format string to be applied to inactive timestamps."
247 :group 'org-export-texinfo
248 :type 'string)
250 (defcustom org-texinfo-diary-timestamp-format "@emph{%s}"
251 "A printf format string to be applied to diary timestamps."
252 :group 'org-export-texinfo
253 :type 'string)
255 ;;;; Links
257 (defcustom org-texinfo-link-with-unknown-path-format "@indicateurl{%s}"
258 "Format string for links with unknown path type."
259 :group 'org-export-texinfo
260 :type 'string)
262 ;;;; Tables
264 (defcustom org-texinfo-tables-verbatim nil
265 "When non-nil, tables are exported verbatim."
266 :group 'org-export-texinfo
267 :type 'boolean)
269 (defcustom org-texinfo-table-scientific-notation "%s\\,(%s)"
270 "Format string to display numbers in scientific notation.
271 The format should have \"%s\" twice, for mantissa and exponent
272 \(i.e. \"%s\\\\times10^{%s}\").
274 When nil, no transformation is made."
275 :group 'org-export-texinfo
276 :type '(choice
277 (string :tag "Format string")
278 (const :tag "No formatting" nil)))
280 (defcustom org-texinfo-def-table-markup "@samp"
281 "Default setting for @table environments."
282 :group 'org-export-texinfo
283 :type 'string)
285 ;;;; Text markup
287 (defcustom org-texinfo-text-markup-alist '((bold . "@strong{%s}")
288 (code . code)
289 (italic . "@emph{%s}")
290 (verbatim . verb)
291 (comment . "@c %s"))
292 "Alist of Texinfo expressions to convert text markup.
294 The key must be a symbol among `bold', `italic' and `comment'.
295 The value is a formatting string to wrap fontified text with.
297 Value can also be set to the following symbols: `verb' and
298 `code'. For the former, Org will use \"@verb\" to
299 create a format string and select a delimiter character that
300 isn't in the string. For the latter, Org will use \"@code\"
301 to typeset and try to protect special characters.
303 If no association can be found for a given markup, text will be
304 returned as-is."
305 :group 'org-export-texinfo
306 :type 'alist
307 :options '(bold code italic verbatim comment))
309 ;;;; Drawers
311 (defcustom org-texinfo-format-drawer-function (lambda (_name contents) contents)
312 "Function called to format a drawer in Texinfo code.
314 The function must accept two parameters:
315 NAME the drawer name, like \"LOGBOOK\"
316 CONTENTS the contents of the drawer.
318 The function should return the string to be exported.
320 The default function simply returns the value of CONTENTS."
321 :group 'org-export-texinfo
322 :version "24.4"
323 :package-version '(Org . "8.2")
324 :type 'function)
326 ;;;; Inlinetasks
328 (defcustom org-texinfo-format-inlinetask-function
329 'org-texinfo-format-inlinetask-default-function
330 "Function called to format an inlinetask in Texinfo code.
332 The function must accept six parameters:
333 TODO the todo keyword, as a string
334 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
335 PRIORITY the inlinetask priority, as a string
336 NAME the inlinetask name, as a string.
337 TAGS the inlinetask tags, as a list of strings.
338 CONTENTS the contents of the inlinetask, as a string.
340 The function should return the string to be exported."
341 :group 'org-export-texinfo
342 :type 'function)
344 ;;;; Compilation
346 (defcustom org-texinfo-info-process '("makeinfo %f")
347 "Commands to process a Texinfo file to an INFO file.
348 This is list of strings, each of them will be given to the shell
349 as a command. %f in the command will be replaced by the full
350 file name, %b by the file base name (i.e without extension) and
351 %o by the base directory of the file."
352 :group 'org-export-texinfo
353 :type '(repeat :tag "Shell command sequence"
354 (string :tag "Shell command")))
356 (defcustom org-texinfo-logfiles-extensions
357 '("aux" "toc" "cp" "fn" "ky" "pg" "tp" "vr")
358 "The list of file extensions to consider as Texinfo logfiles.
359 The logfiles will be remove if `org-texinfo-remove-logfiles' is
360 non-nil."
361 :group 'org-export-texinfo
362 :type '(repeat (string :tag "Extension")))
364 (defcustom org-texinfo-remove-logfiles t
365 "Non-nil means remove the logfiles produced by compiling a Texinfo file.
366 By default, logfiles are files with these extensions: .aux, .toc,
367 .cp, .fn, .ky, .pg and .tp. To define the set of logfiles to remove,
368 set `org-texinfo-logfiles-extensions'."
369 :group 'org-export-latex
370 :type 'boolean)
372 ;;; Constants
374 (defconst org-texinfo-max-toc-depth 4
375 "Maximum depth for creation of detailed menu listings.
376 Beyond this depth, Texinfo will not recognize the nodes and will
377 cause errors. Left as a constant in case this value ever
378 changes.")
380 (defconst org-texinfo-supported-coding-systems
381 '("US-ASCII" "UTF-8" "ISO-8859-15" "ISO-8859-1" "ISO-8859-2" "koi8-r" "koi8-u")
382 "List of coding systems supported by Texinfo, as strings.
383 Specified coding system will be matched against these strings.
384 If two strings share the same prefix (e.g. \"ISO-8859-1\" and
385 \"ISO-8859-15\"), the most specific one has to be listed first.")
387 (defconst org-texinfo-inline-image-rules
388 (list (cons "file"
389 (regexp-opt '("eps" "pdf" "png" "jpg" "jpeg" "gif" "svg"))))
390 "Rules characterizing image files that can be inlined.")
393 ;;; Internal Functions
395 (defun org-texinfo--filter-section-blank-lines (headline _backend _info)
396 "Filter controlling number of blank lines after a section."
397 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" "\n\n" headline))
399 (defun org-texinfo--normalize-headlines (tree _backend info)
400 "Normalize headlines in TREE.
402 BACK-END is the symbol specifying back-end used for export. INFO
403 is a plist used as a communication channel.
405 Make sure every headline in TREE contains a section, since those
406 are required to install a menu. Also put exactly one blank line
407 at the end of each section.
409 Return new tree."
410 (org-element-map tree 'headline
411 (lambda (hl)
412 (org-element-put-property hl :post-blank 1)
413 (let ((contents (org-element-contents hl)))
414 (when contents
415 (let ((first (org-element-map contents '(headline section)
416 #'identity info t)))
417 (unless (eq (org-element-type first) 'section)
418 (apply #'org-element-set-contents
420 (cons `(section (:parent ,hl)) contents)))))))
421 info)
422 tree)
424 (defun org-texinfo--find-verb-separator (s)
425 "Return a character not used in string S.
426 This is used to choose a separator for constructs like \\verb."
427 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
428 (loop for c across ll
429 when (not (string-match (regexp-quote (char-to-string c)) s))
430 return (char-to-string c))))
432 (defun org-texinfo--text-markup (text markup _info)
433 "Format TEXT depending on MARKUP text markup.
434 INFO is a plist used as a communication channel. See
435 `org-texinfo-text-markup-alist' for details."
436 (pcase (cdr (assq markup org-texinfo-text-markup-alist))
437 ;; No format string: Return raw text.
438 (`nil text)
439 (`verb
440 (let ((separator (org-texinfo--find-verb-separator text)))
441 (concat "@verb{" separator text separator "}")))
442 (`code
443 (format "@code{%s}" (replace-regexp-in-string "[@{}]" "@\\&" text)))
444 ;; Else use format string.
445 (fmt (format fmt text))))
447 (defun org-texinfo--get-node (blob info)
448 "Return node or anchor associated to BLOB.
449 BLOB is an element or object. INFO is a plist used as
450 a communication channel. The function guarantees the node or
451 anchor name is unique."
452 (let ((cache (plist-get info :texinfo-node-cache)))
453 (or (cdr (assq blob cache))
454 (let ((name
455 (org-texinfo--sanitize-node
456 (if (eq (org-element-type blob) 'headline)
457 (org-export-data (org-export-get-alt-title blob info) info)
458 (org-export-get-reference blob info)))))
459 ;; Ensure NAME is unique.
460 (while (rassoc name cache) (setq name (concat name "x")))
461 (plist-put info :texinfo-node-cache (cons (cons blob name) cache))
462 name))))
464 (defun org-texinfo--sanitize-node (title)
465 "Bend string TITLE to node line requirements.
466 Trim string and collapse multiple whitespace characters as they
467 are not significant. Also remove the following characters: @
468 { } ( ) : . ,"
469 (replace-regexp-in-string
470 "[:,.]" ""
471 (replace-regexp-in-string
472 "\\`(\\(.*)\\)" "[\\1"
473 (org-trim (replace-regexp-in-string "[ \t]\\{2,\\}" " " title)))))
475 (defun org-texinfo--sanitize-content (text)
476 "Escape special characters in string TEXT.
477 Special characters are: @ { }"
478 (replace-regexp-in-string "[@{}]" "@\\&" text))
480 (defun org-texinfo--wrap-float (value info &optional type label caption short)
481 "Wrap string VALUE within a @float command.
482 INFO is the current export state, as a plist. TYPE is float
483 type, as a string. LABEL is the cross reference label for the
484 float, as a string. CAPTION and SHORT are, respectively, the
485 caption and shortcaption used for the float, as secondary
486 strings (e.g., returned by `org-export-get-caption')."
487 (let* ((backend
488 (org-export-create-backend
489 :parent 'texinfo
490 :transcoders '((link . (lambda (object c i) c))
491 (radio-target . (lambda (object c i) c))
492 (target . ignore))))
493 (short-backend
494 (org-export-create-backend
495 :parent 'texinfo
496 :transcoders '((footnote-reference . ignore)
497 (inline-src-block . ignore)
498 (link . (lambda (object c i) c))
499 (radio-target . (lambda (object c i) c))
500 (target . ignore)
501 (verbatim . ignore))))
502 (short-str
503 (if (and short caption)
504 (format "@shortcaption{%s}\n"
505 (org-export-data-with-backend short short-backend info))
506 ""))
507 (caption-str
508 (if (or short caption)
509 (format "@caption{%s}\n"
510 (org-export-data-with-backend
511 (or caption short)
512 (if (equal short-str "") short-backend backend)
513 info))
514 "")))
515 (format "@float %s%s\n%s\n%s%s@end float"
516 type (if label (concat "," label) "") value caption-str short-str)))
518 ;;; Template
520 (defun org-texinfo-template (contents info)
521 "Return complete document string after Texinfo conversion.
522 CONTENTS is the transcoded contents string. INFO is a plist
523 holding export options."
524 (let ((title (org-export-data (plist-get info :title) info))
525 ;; Copying data is the contents of the first headline in
526 ;; parse tree with a non-nil copying property.
527 (copying (org-element-map (plist-get info :parse-tree) 'headline
528 (lambda (hl)
529 (and (org-not-nil (org-element-property :COPYING hl))
530 (org-element-contents hl)))
531 info t)))
532 (concat
533 "\\input texinfo @c -*- texinfo -*-\n"
534 "@c %**start of header\n"
535 (let ((file (or (plist-get info :texinfo-filename)
536 (let ((f (plist-get info :output-file)))
537 (and f (concat (file-name-sans-extension f) ".info"))))))
538 (and file (format "@setfilename %s\n" file)))
539 (format "@settitle %s\n" title)
540 ;; Insert class-defined header.
541 (org-element-normalize-string
542 (let ((header (nth 1 (assoc (plist-get info :texinfo-class)
543 org-texinfo-classes)))
544 (coding
545 (catch 'coding-system
546 (let ((case-fold-search t)
547 (name (symbol-name (or org-texinfo-coding-system
548 buffer-file-coding-system))))
549 (dolist (system org-texinfo-supported-coding-systems "UTF-8")
550 (when (org-string-match-p (regexp-quote system) name)
551 (throw 'coding-system system))))))
552 (language (plist-get info :language))
553 (case-fold-search nil))
554 ;; Auto coding system.
555 (replace-regexp-in-string
556 "^@documentencoding \\(AUTO\\)$"
557 coding
558 (replace-regexp-in-string
559 "^@documentlanguage \\(AUTO\\)$" language header t nil 1) t nil 1)))
560 ;; Additional header options set by #+TEXINFO_HEADER.
561 (let ((texinfo-header (plist-get info :texinfo-header)))
562 (and texinfo-header (org-element-normalize-string texinfo-header)))
563 "@c %**end of header\n\n"
564 ;; Additional options set by #+TEXINFO_POST_HEADER.
565 (let ((texinfo-post-header (plist-get info :texinfo-post-header)))
566 (and texinfo-post-header
567 (org-element-normalize-string texinfo-post-header)))
568 ;; Copying.
569 (and copying
570 (format "@copying\n%s@end copying\n\n"
571 (org-element-normalize-string
572 (org-export-data copying info))))
573 ;; Info directory information. Only supply if both title and
574 ;; category are provided.
575 (let ((dircat (plist-get info :texinfo-dircat))
576 (dirtitle
577 (let ((title (plist-get info :texinfo-dirtitle)))
578 (and title
579 (string-match "^\\(?:\\* \\)?\\(.*?\\)\\(\\.\\)?$" title)
580 (format "* %s." (match-string 1 title))))))
581 (when (and dircat dirtitle)
582 (concat "@dircategory " dircat "\n"
583 "@direntry\n"
584 (let ((dirdesc
585 (let ((desc (plist-get info :texinfo-dirdesc)))
586 (cond ((not desc) nil)
587 ((org-string-match-p "\\.$" desc) desc)
588 (t (concat desc "."))))))
589 (if dirdesc (format "%-23s %s" dirtitle dirdesc) dirtitle))
590 "\n"
591 "@end direntry\n\n")))
592 ;; Title
593 "@finalout\n"
594 "@titlepage\n"
595 (when (plist-get info :with-title)
596 (concat
597 (format "@title %s\n" (or (plist-get info :texinfo-printed-title) title ""))
598 (let ((subtitle (plist-get info :subtitle)))
599 (when subtitle
600 (format "@subtitle %s\n"
601 (org-export-data subtitle info))))))
602 (when (plist-get info :with-author)
603 (concat
604 ;; Primary author.
605 (let ((author (org-string-nw-p
606 (org-export-data (plist-get info :author) info)))
607 (email (and (plist-get info :with-email)
608 (org-string-nw-p
609 (org-export-data (plist-get info :email) info)))))
610 (cond ((and author email)
611 (format "@author %s (@email{%s})\n" author email))
612 (author (format "@author %s\n" author))
613 (email (format "@author @email{%s}\n" email))))
614 ;; Other authors.
615 (let ((subauthor (plist-get info :subauthor)))
616 (and subauthor
617 (org-element-normalize-string
618 (replace-regexp-in-string "^" "@author " subauthor))))))
619 (and copying "@page\n@vskip 0pt plus 1filll\n@insertcopying\n")
620 "@end titlepage\n\n"
621 ;; Table of contents.
622 (and (plist-get info :with-toc) "@contents\n\n")
623 ;; Configure Top Node when not for Tex
624 "@ifnottex\n"
625 "@node Top\n"
626 (format "@top %s\n" title)
627 (and copying "@insertcopying\n")
628 "@end ifnottex\n\n"
629 ;; Menu.
630 (org-texinfo-make-menu (plist-get info :parse-tree) info 'master)
631 "\n"
632 ;; Document's body.
633 contents "\n"
634 ;; Creator.
635 (and (plist-get info :with-creator)
636 (concat (plist-get info :creator) "\n"))
637 ;; Document end.
638 "@bye")))
642 ;;; Transcode Functions
644 ;;;; Bold
646 (defun org-texinfo-bold (_bold contents info)
647 "Transcode BOLD from Org to Texinfo.
648 CONTENTS is the text with bold markup. INFO is a plist holding
649 contextual information."
650 (org-texinfo--text-markup contents 'bold info))
652 ;;;; Center Block
654 (defun org-texinfo-center-block (_center-block contents _info)
655 "Transcode a CENTER-BLOCK element from Org to Texinfo.
656 CONTENTS holds the contents of the block. INFO is a plist used
657 as a communication channel."
658 contents)
660 ;;;; Clock
662 (defun org-texinfo-clock (clock _contents info)
663 "Transcode a CLOCK element from Org to Texinfo.
664 CONTENTS is nil. INFO is a plist holding contextual
665 information."
666 (concat
667 "@noindent"
668 (format "@strong{%s} " org-clock-string)
669 (format (plist-get info :texinfo-inactive-timestamp-format)
670 (concat (org-timestamp-translate (org-element-property :value clock))
671 (let ((time (org-element-property :duration clock)))
672 (and time (format " (%s)" time)))))
673 "@*"))
675 ;;;; Code
677 (defun org-texinfo-code (code _contents info)
678 "Transcode a CODE object from Org to Texinfo.
679 CONTENTS is nil. INFO is a plist used as a communication
680 channel."
681 (org-texinfo--text-markup (org-element-property :value code) 'code info))
683 ;;;; Drawer
685 (defun org-texinfo-drawer (drawer contents info)
686 "Transcode a DRAWER element from Org to Texinfo.
687 CONTENTS holds the contents of the block. INFO is a plist
688 holding contextual information."
689 (let* ((name (org-element-property :drawer-name drawer))
690 (output (funcall (plist-get info :texinfo-format-drawer-function)
691 name contents)))
692 output))
694 ;;;; Dynamic Block
696 (defun org-texinfo-dynamic-block (_dynamic-block contents _info)
697 "Transcode a DYNAMIC-BLOCK element from Org to Texinfo.
698 CONTENTS holds the contents of the block. INFO is a plist
699 holding contextual information."
700 contents)
702 ;;;; Entity
704 (defun org-texinfo-entity (entity _contents _info)
705 "Transcode an ENTITY object from Org to Texinfo.
706 CONTENTS are the definition itself. INFO is a plist holding
707 contextual information."
708 (let ((ent (org-element-property :latex entity)))
709 (if (org-element-property :latex-math-p entity) (format "@math{%s}" ent) ent)))
711 ;;;; Example Block
713 (defun org-texinfo-example-block (example-block _contents info)
714 "Transcode an EXAMPLE-BLOCK element from Org to Texinfo.
715 CONTENTS is nil. INFO is a plist holding contextual
716 information."
717 (format "@verbatim\n%s@end verbatim"
718 (org-export-format-code-default example-block info)))
720 ;;; Export Block
722 (defun org-texinfo-export-block (export-block _contents _info)
723 "Transcode a EXPORT-BLOCK element from Org to Texinfo.
724 CONTENTS is nil. INFO is a plist holding contextual information."
725 (when (string= (org-element-property :type export-block) "TEXINFO")
726 (org-remove-indentation (org-element-property :value export-block))))
728 ;;; Export Snippet
730 (defun org-texinfo-export-snippet (export-snippet _contents _info)
731 "Transcode a EXPORT-SNIPPET object from Org to Texinfo.
732 CONTENTS is nil. INFO is a plist holding contextual information."
733 (when (eq (org-export-snippet-backend export-snippet) 'texinfo)
734 (org-element-property :value export-snippet)))
736 ;;;; Fixed Width
738 (defun org-texinfo-fixed-width (fixed-width _contents _info)
739 "Transcode a FIXED-WIDTH element from Org to Texinfo.
740 CONTENTS is nil. INFO is a plist holding contextual information."
741 (format "@example\n%s\n@end example"
742 (org-remove-indentation
743 (org-texinfo--sanitize-content
744 (org-element-property :value fixed-width)))))
746 ;;;; Footnote Reference
748 (defun org-texinfo-footnote-reference (footnote _contents info)
749 "Create a footnote reference for FOOTNOTE.
751 FOOTNOTE is the footnote to define. CONTENTS is nil. INFO is a
752 plist holding contextual information."
753 (let ((def (org-export-get-footnote-definition footnote info)))
754 (format "@footnote{%s}"
755 (org-trim (org-export-data def info)))))
757 ;;;; Headline
759 (defun org-texinfo-headline (headline contents info)
760 "Transcode a HEADLINE element from Org to Texinfo.
761 CONTENTS holds the contents of the headline. INFO is a plist
762 holding contextual information."
763 (let* ((class (plist-get info :texinfo-class))
764 (level (org-export-get-relative-level headline info))
765 (numberedp (org-export-numbered-headline-p headline info))
766 (class-sectioning (assoc class (plist-get info :texinfo-classes)))
767 ;; Find the index type, if any.
768 (index (org-element-property :INDEX headline))
769 ;; Create node info, to insert it before section formatting.
770 ;; Use custom menu title if present.
771 (node (format "@node %s\n" (org-texinfo--get-node headline info)))
772 ;; Section formatting will set two placeholders: one for the
773 ;; title and the other for the contents.
774 (section-fmt
775 (if (org-not-nil (org-element-property :APPENDIX headline))
776 "@appendix %s\n%s"
777 (let ((sec (if (and (symbolp (nth 2 class-sectioning))
778 (fboundp (nth 2 class-sectioning)))
779 (funcall (nth 2 class-sectioning) level numberedp)
780 (nth (1+ level) class-sectioning))))
781 (cond
782 ;; No section available for that LEVEL.
783 ((not sec) nil)
784 ;; Section format directly returned by a function.
785 ((stringp sec) sec)
786 ;; (numbered-section . unnumbered-section)
787 ((not (consp (cdr sec)))
788 (concat (if (or index (not numberedp)) (cdr sec) (car sec))
789 "\n%s"))))))
790 (todo
791 (and (plist-get info :with-todo-keywords)
792 (let ((todo (org-element-property :todo-keyword headline)))
793 (and todo (org-export-data todo info)))))
794 (todo-type (and todo (org-element-property :todo-type headline)))
795 (tags (and (plist-get info :with-tags)
796 (org-export-get-tags headline info)))
797 (priority (and (plist-get info :with-priority)
798 (org-element-property :priority headline)))
799 (text (org-export-data (org-element-property :title headline) info))
800 (full-text (funcall (plist-get info :texinfo-format-headline-function)
801 todo todo-type priority text tags))
802 (contents (if (org-string-nw-p contents) (concat "\n" contents) "")))
803 (cond
804 ;; Case 1: This is a footnote section: ignore it.
805 ((org-element-property :footnote-section-p headline) nil)
806 ;; Case 2: This is the `copying' section: ignore it
807 ;; This is used elsewhere.
808 ((org-not-nil (org-element-property :COPYING headline)) nil)
809 ;; Case 3: An index. If it matches one of the known indexes,
810 ;; print it as such following the contents, otherwise
811 ;; print the contents and leave the index up to the user.
812 (index
813 (concat node
814 (format
815 section-fmt
816 full-text
817 (concat contents
818 (and (member index '("cp" "fn" "ky" "pg" "tp" "vr"))
819 (concat "\n@printindex " index))))))
820 ;; Case 4: This is a deep sub-tree: export it as a list item.
821 ;; Also export as items headlines for which no section
822 ;; format has been found.
823 ((or (not section-fmt) (org-export-low-level-p headline info))
824 ;; Build the real contents of the sub-tree.
825 (concat (and (org-export-first-sibling-p headline info)
826 (format "@%s\n" (if numberedp 'enumerate 'itemize)))
827 "@item\n" full-text "\n"
828 contents
829 (if (org-export-last-sibling-p headline info)
830 (format "@end %s" (if numberedp 'enumerate 'itemize))
831 "\n")))
832 ;; Case 5: Standard headline. Export it as a section.
833 (t (concat node (format section-fmt full-text contents))))))
835 (defun org-texinfo-format-headline-default-function
836 (todo _todo-type priority text tags)
837 "Default format function for a headline.
838 See `org-texinfo-format-headline-function' for details."
839 (concat (when todo (format "@strong{%s} " todo))
840 (when priority (format "@emph{#%s} " priority))
841 text
842 (when tags (format " :%s:" (mapconcat 'identity tags ":")))))
844 ;;;; Inline Src Block
846 (defun org-texinfo-inline-src-block (inline-src-block _contents _info)
847 "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
848 CONTENTS holds the contents of the item. INFO is a plist holding
849 contextual information."
850 (let* ((code (org-element-property :value inline-src-block))
851 (separator (org-texinfo--find-verb-separator code)))
852 (concat "@verb{" separator code separator "}")))
854 ;;;; Inlinetask
856 (defun org-texinfo-inlinetask (inlinetask contents info)
857 "Transcode an INLINETASK element from Org to Texinfo.
858 CONTENTS holds the contents of the block. INFO is a plist
859 holding contextual information."
860 (let ((title (org-export-data (org-element-property :title inlinetask) info))
861 (todo (and (plist-get info :with-todo-keywords)
862 (let ((todo (org-element-property :todo-keyword inlinetask)))
863 (and todo (org-export-data todo info)))))
864 (todo-type (org-element-property :todo-type inlinetask))
865 (tags (and (plist-get info :with-tags)
866 (org-export-get-tags inlinetask info)))
867 (priority (and (plist-get info :with-priority)
868 (org-element-property :priority inlinetask))))
869 (funcall (plist-get info :texinfo-format-inlinetask-function)
870 todo todo-type priority title tags contents)))
872 (defun org-texinfo-format-inlinetask-default-function
873 (todo _todo-type priority title tags contents)
874 "Default format function for a inlinetasks.
875 See `org-texinfo-format-inlinetask-function' for details."
876 (let ((full-title
877 (concat (when todo (format "@strong{%s} " todo))
878 (when priority (format "#%c " priority))
879 title
880 (when tags (format ":%s:" (mapconcat #'identity tags ":"))))))
881 (format "@center %s\n\n%s\n" full-title contents)))
883 ;;;; Italic
885 (defun org-texinfo-italic (_italic contents info)
886 "Transcode ITALIC from Org to Texinfo.
887 CONTENTS is the text with italic markup. INFO is a plist holding
888 contextual information."
889 (org-texinfo--text-markup contents 'italic info))
891 ;;;; Item
893 (defun org-texinfo-item (item contents info)
894 "Transcode an ITEM element from Org to Texinfo.
895 CONTENTS holds the contents of the item. INFO is a plist holding
896 contextual information."
897 (format "@item%s\n%s"
898 (let ((tag (org-element-property :tag item)))
899 (if tag (concat " " (org-export-data tag info)) ""))
900 (or contents "")))
902 ;;;; Keyword
904 (defun org-texinfo-keyword (keyword _contents info)
905 "Transcode a KEYWORD element from Org to Texinfo.
906 CONTENTS is nil. INFO is a plist holding contextual information."
907 (let ((key (org-element-property :key keyword))
908 (value (org-element-property :value keyword)))
909 (cond
910 ((string= key "TEXINFO") value)
911 ((string= key "CINDEX") (format "@cindex %s" value))
912 ((string= key "FINDEX") (format "@findex %s" value))
913 ((string= key "KINDEX") (format "@kindex %s" value))
914 ((string= key "PINDEX") (format "@pindex %s" value))
915 ((string= key "TINDEX") (format "@tindex %s" value))
916 ((string= key "VINDEX") (format "@vindex %s" value))
917 ((string= key "TOC")
918 (cond ((org-string-match-p "\\<tables\\>" value)
919 (concat "@listoffloats "
920 (org-export-translate "Table" :utf-8 info)))
921 ((org-string-match-p "\\<listings\\>" value)
922 (concat "@listoffloats "
923 (org-export-translate "Listing" :utf-8 info))))))))
925 ;;;; Line Break
927 (defun org-texinfo-line-break (_line-break _contents _info)
928 "Transcode a LINE-BREAK object from Org to Texinfo.
929 CONTENTS is nil. INFO is a plist holding contextual information."
930 "@*\n")
932 ;;;; Link
934 (defun org-texinfo-link (link desc info)
935 "Transcode a LINK object from Org to Texinfo.
937 DESC is the description part of the link, or the empty string.
938 INFO is a plist holding contextual information. See
939 `org-export-data'."
940 (let* ((type (org-element-property :type link))
941 (raw-path (org-element-property :path link))
942 ;; Ensure DESC really exists, or set it to nil.
943 (desc (and (not (string= desc "")) desc))
944 (path (cond
945 ((member type '("http" "https" "ftp"))
946 (concat type ":" raw-path))
947 ((string= type "file") (org-export-file-uri raw-path))
948 (t raw-path))))
949 (cond
950 ((org-export-custom-protocol-maybe link desc 'texinfo))
951 ((org-export-inline-image-p link org-texinfo-inline-image-rules)
952 (org-texinfo--inline-image link info))
953 ((equal type "radio")
954 (let ((destination (org-export-resolve-radio-link link info)))
955 (if (not destination) desc
956 (format "@ref{%s,,%s}"
957 (org-texinfo--get-node destination info)
958 desc))))
959 ((member type '("custom-id" "id" "fuzzy"))
960 (let ((destination
961 (if (equal type "fuzzy")
962 (org-export-resolve-fuzzy-link link info)
963 (org-export-resolve-id-link link info))))
964 (case (org-element-type destination)
965 ((nil)
966 (format org-texinfo-link-with-unknown-path-format
967 (org-texinfo--sanitize-content path)))
968 ;; Id link points to an external file.
969 (plain-text
970 (if desc (format "@uref{file://%s,%s}" destination desc)
971 (format "@uref{file://%s}" destination)))
972 (headline
973 (format "@ref{%s,%s}"
974 (org-texinfo--get-node destination info)
975 (cond
976 (desc)
977 ((org-export-numbered-headline-p destination info)
978 (mapconcat
979 #'number-to-string
980 (org-export-get-headline-number destination info) "."))
981 (t (org-export-data
982 (org-element-property :title destination) info)))))
983 (otherwise
984 (format "@ref{%s,,%s}"
985 (org-texinfo--get-node destination info)
986 (cond
987 (desc)
988 ;; No description is provided: first try to
989 ;; associate destination to a number.
990 ((let ((n (org-export-get-ordinal destination info)))
991 (cond ((not n) nil)
992 ((integerp n) n)
993 (t (mapconcat #'number-to-string n ".")))))
994 ;; Then grab title of headline containing
995 ;; DESTINATION.
996 ((let ((h (org-element-lineage destination '(headline) t)))
997 (and h
998 (org-export-data
999 (org-element-property :title destination) info))))
1000 ;; Eventually, just return "Top" to refer to the
1001 ;; beginning of the info file.
1002 (t "Top")))))))
1003 ((equal type "info")
1004 (let* ((info-path (split-string path "[:#]"))
1005 (info-manual (car info-path))
1006 (info-node (or (cadr info-path) "Top"))
1007 (title (or desc "")))
1008 (format "@ref{%s,%s,,%s,}" info-node title info-manual)))
1009 ((string= type "mailto")
1010 (format "@email{%s}"
1011 (concat (org-texinfo--sanitize-content path)
1012 (and desc (concat "," desc)))))
1013 ;; External link with a description part.
1014 ((and path desc) (format "@uref{%s,%s}" path desc))
1015 ;; External link without a description part.
1016 (path (format "@uref{%s}" path))
1017 ;; No path, only description. Try to do something useful.
1019 (format (plist-get info :texinfo-link-with-unknown-path-format) desc)))))
1021 (defun org-texinfo--inline-image (link info)
1022 "Return Texinfo code for an inline image.
1023 LINK is the link pointing to the inline image. INFO is the
1024 current state of the export, as a plist."
1025 (let* ((parent (org-export-get-parent-element link))
1026 (label (and (org-element-property :name parent)
1027 (org-texinfo--get-node parent info)))
1028 (caption (org-export-get-caption parent))
1029 (shortcaption (org-export-get-caption parent t))
1030 (path (org-element-property :path link))
1031 (filename
1032 (file-name-sans-extension
1033 (if (file-name-absolute-p path) (expand-file-name path) path)))
1034 (extension (file-name-extension path))
1035 (attributes (org-export-read-attribute :attr_texinfo parent))
1036 (height (or (plist-get attributes :height) ""))
1037 (width (or (plist-get attributes :width) ""))
1038 (alt (or (plist-get attributes :alt) ""))
1039 (image (format "@image{%s,%s,%s,%s,%s}"
1040 filename width height alt extension)))
1041 (cond ((or caption shortcaption)
1042 (org-texinfo--wrap-float image
1043 info
1044 (org-export-translate "Figure" :utf-8 info)
1045 label
1046 caption
1047 shortcaption))
1048 (label (concat "@anchor{" label "}\n" image))
1049 (t image))))
1052 ;;;; Menu
1054 (defun org-texinfo-make-menu (scope info &optional master)
1055 "Create the menu for inclusion in the Texinfo document.
1057 SCOPE is a headline or a full parse tree. INFO is the
1058 communication channel, as a plist.
1060 When optional argument MASTER is non-nil, generate a master menu,
1061 including detailed node listing."
1062 (let ((menu (org-texinfo--build-menu scope info)))
1063 (when (org-string-nw-p menu)
1064 (org-element-normalize-string
1065 (format
1066 "@menu\n%s@end menu"
1067 (concat menu
1068 (when master
1069 (let ((detailmenu
1070 (org-texinfo--build-menu
1071 scope info
1072 (let ((toc-depth (plist-get info :with-toc)))
1073 (if (wholenump toc-depth) toc-depth
1074 org-texinfo-max-toc-depth)))))
1075 (when (org-string-nw-p detailmenu)
1076 (concat "\n@detailmenu\n"
1077 "--- The Detailed Node Listing ---\n\n"
1078 detailmenu
1079 "@end detailmenu\n"))))))))))
1081 (defun org-texinfo--build-menu (scope info &optional level)
1082 "Build menu for entries within SCOPE.
1083 SCOPE is a headline or a full parse tree. INFO is a plist
1084 containing contextual information. When optional argument LEVEL
1085 is an integer, build the menu recursively, down to this depth."
1086 (cond
1087 ((not level)
1088 (org-texinfo--format-entries (org-texinfo--menu-entries scope info) info))
1089 ((zerop level) nil)
1091 (org-element-normalize-string
1092 (mapconcat
1093 (lambda (h)
1094 (let ((entries (org-texinfo--menu-entries h info)))
1095 (when entries
1096 (concat
1097 (format "%s\n\n%s\n"
1098 (org-export-data (org-export-get-alt-title h info) info)
1099 (org-texinfo--format-entries entries info))
1100 (org-texinfo--build-menu h info (1- level))))))
1101 (org-texinfo--menu-entries scope info) "")))))
1103 (defun org-texinfo--format-entries (entries info)
1104 "Format all direct menu entries in SCOPE, as a string.
1105 SCOPE is either a headline or a full Org document. INFO is
1106 a plist containing contextual information."
1107 (org-element-normalize-string
1108 (mapconcat
1109 (lambda (h)
1110 (let* ((title (org-export-data
1111 (org-export-get-alt-title h info) info))
1112 (node (org-texinfo--get-node h info))
1113 (entry (concat "* " title ":"
1114 (if (string= title node) ":"
1115 (concat " " node ". "))))
1116 (desc (org-element-property :DESCRIPTION h)))
1117 (if (not desc) entry
1118 (format (format "%%-%ds %%s" org-texinfo-node-description-column)
1119 entry desc))))
1120 entries "\n")))
1122 (defun org-texinfo--menu-entries (scope info)
1123 "List direct children in SCOPE needing a menu entry.
1124 SCOPE is a headline or a full parse tree. INFO is a plist
1125 holding contextual information."
1126 (let* ((cache (or (plist-get info :texinfo-entries-cache)
1127 (plist-get (plist-put info :texinfo-entries-cache
1128 (make-hash-table :test #'eq))
1129 :texinfo-entries-cache)))
1130 (cached-entries (gethash scope cache 'no-cache)))
1131 (if (not (eq cached-entries 'no-cache)) cached-entries
1132 (puthash scope
1133 (org-element-map (org-element-contents scope) 'headline
1134 (lambda (h)
1135 (and (not (org-not-nil (org-element-property :COPYING h)))
1136 (not (org-element-property :footnote-section-p h))
1137 (not (org-export-low-level-p h info))
1139 info nil 'headline)
1140 cache))))
1142 ;;;; Node Property
1144 (defun org-texinfo-node-property (node-property _contents _info)
1145 "Transcode a NODE-PROPERTY element from Org to Texinfo.
1146 CONTENTS is nil. INFO is a plist holding contextual
1147 information."
1148 (format "%s:%s"
1149 (org-element-property :key node-property)
1150 (let ((value (org-element-property :value node-property)))
1151 (if value (concat " " value) ""))))
1153 ;;;; Paragraph
1155 (defun org-texinfo-paragraph (_paragraph contents _info)
1156 "Transcode a PARAGRAPH element from Org to Texinfo.
1157 CONTENTS is the contents of the paragraph, as a string. INFO is
1158 the plist used as a communication channel."
1159 contents)
1161 ;;;; Plain List
1163 (defun org-texinfo-plain-list (plain-list contents info)
1164 "Transcode a PLAIN-LIST element from Org to Texinfo.
1165 CONTENTS is the contents of the list. INFO is a plist holding
1166 contextual information."
1167 (let* ((attr (org-export-read-attribute :attr_texinfo plain-list))
1168 (indic (or (plist-get attr :indic)
1169 (plist-get info :texinfo-def-table-markup)))
1170 (table-type (plist-get attr :table-type))
1171 (type (org-element-property :type plain-list))
1172 (list-type (cond
1173 ((eq type 'ordered) "enumerate")
1174 ((eq type 'unordered) "itemize")
1175 ((member table-type '("ftable" "vtable")) table-type)
1176 (t "table"))))
1177 (format "@%s\n%s@end %s"
1178 (if (eq type 'descriptive) (concat list-type " " indic) list-type)
1179 contents
1180 list-type)))
1182 ;;;; Plain Text
1184 (defun org-texinfo-plain-text (text info)
1185 "Transcode a TEXT string from Org to Texinfo.
1186 TEXT is the string to transcode. INFO is a plist holding
1187 contextual information."
1188 ;; First protect @, { and }.
1189 (let ((output (org-texinfo--sanitize-content text)))
1190 ;; Activate smart quotes. Be sure to provide original TEXT string
1191 ;; since OUTPUT may have been modified.
1192 (when (plist-get info :with-smart-quotes)
1193 (setq output
1194 (org-export-activate-smart-quotes output :texinfo info text)))
1195 ;; LaTeX into @LaTeX{} and TeX into @TeX{}
1196 (let ((case-fold-search nil)
1197 (start 0))
1198 (while (string-match "\\(\\(?:La\\)?TeX\\)" output start)
1199 (setq output (replace-match
1200 (format "@%s{}" (match-string 1 output)) nil t output)
1201 start (match-end 0))))
1202 ;; Convert special strings.
1203 (when (plist-get info :with-special-strings)
1204 (while (string-match (regexp-quote "...") output)
1205 (setq output (replace-match "@dots{}" nil t output))))
1206 ;; Handle break preservation if required.
1207 (when (plist-get info :preserve-breaks)
1208 (setq output (replace-regexp-in-string
1209 "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" output)))
1210 ;; Return value.
1211 output))
1213 ;;;; Planning
1215 (defun org-texinfo-planning (planning _contents info)
1216 "Transcode a PLANNING element from Org to Texinfo.
1217 CONTENTS is nil. INFO is a plist holding contextual
1218 information."
1219 (concat
1220 "@noindent"
1221 (mapconcat
1222 'identity
1223 (delq nil
1224 (list
1225 (let ((closed (org-element-property :closed planning)))
1226 (when closed
1227 (concat
1228 (format "@strong{%s} " org-closed-string)
1229 (format (plist-get info :texinfo-inactive-timestamp-format)
1230 (org-timestamp-translate closed)))))
1231 (let ((deadline (org-element-property :deadline planning)))
1232 (when deadline
1233 (concat
1234 (format "@strong{%s} " org-deadline-string)
1235 (format (plist-get info :texinfo-active-timestamp-format)
1236 (org-timestamp-translate deadline)))))
1237 (let ((scheduled (org-element-property :scheduled planning)))
1238 (when scheduled
1239 (concat
1240 (format "@strong{%s} " org-scheduled-string)
1241 (format (plist-get info :texinfo-active-timestamp-format)
1242 (org-timestamp-translate scheduled)))))))
1243 " ")
1244 "@*"))
1246 ;;;; Property Drawer
1248 (defun org-texinfo-property-drawer (_property-drawer contents _info)
1249 "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
1250 CONTENTS holds the contents of the drawer. INFO is a plist
1251 holding contextual information."
1252 (and (org-string-nw-p contents)
1253 (format "@verbatim\n%s@end verbatim" contents)))
1255 ;;;; Quote Block
1257 (defun org-texinfo-quote-block (quote-block contents _info)
1258 "Transcode a QUOTE-BLOCK element from Org to Texinfo.
1259 CONTENTS holds the contents of the block. INFO is a plist
1260 holding contextual information."
1261 (let* ((title (org-element-property :name quote-block))
1262 (start-quote (concat "@quotation"
1263 (if title
1264 (format " %s" title)))))
1265 (format "%s\n%s@end quotation" start-quote contents)))
1267 ;;;; Radio Target
1269 (defun org-texinfo-radio-target (radio-target text info)
1270 "Transcode a RADIO-TARGET object from Org to Texinfo.
1271 TEXT is the text of the target. INFO is a plist holding
1272 contextual information."
1273 (format "@anchor{%s}%s"
1274 (org-export-get-reference radio-target info)
1275 text))
1277 ;;;; Section
1279 (defun org-texinfo-section (section contents info)
1280 "Transcode a SECTION element from Org to Texinfo.
1281 CONTENTS holds the contents of the section. INFO is a plist
1282 holding contextual information."
1283 (concat contents
1284 (let ((parent (org-export-get-parent-headline section)))
1285 (and parent (org-texinfo-make-menu parent info)))))
1287 ;;;; Special Block
1289 (defun org-texinfo-special-block (special-block contents _info)
1290 "Transcode a SPECIAL-BLOCK element from Org to Texinfo.
1291 CONTENTS holds the contents of the block. INFO is a plist used
1292 as a communication channel."
1293 (let ((type (org-element-property :type special-block)))
1294 (format "@%s\n%s@end %s" type contents type)))
1296 ;;;; Src Block
1298 (defun org-texinfo-src-block (src-block _contents info)
1299 "Transcode a SRC-BLOCK element from Org to Texinfo.
1300 CONTENTS holds the contents of the item. INFO is a plist holding
1301 contextual information."
1302 (let* ((lisp (org-string-match-p "lisp"
1303 (org-element-property :language src-block)))
1304 (code (org-texinfo--sanitize-content
1305 (org-export-format-code-default src-block info)))
1306 (value (format
1307 (if lisp "@lisp\n%s@end lisp" "@example\n%s@end example")
1308 code))
1309 (caption (org-export-get-caption src-block))
1310 (shortcaption (org-export-get-caption src-block t)))
1311 (if (not (or caption shortcaption)) value
1312 (org-texinfo--wrap-float value
1313 info
1314 (org-export-translate "Listing" :utf-8 info)
1315 (org-export-get-reference src-block info)
1316 caption
1317 shortcaption))))
1319 ;;;; Statistics Cookie
1321 (defun org-texinfo-statistics-cookie (statistics-cookie _contents _info)
1322 "Transcode a STATISTICS-COOKIE object from Org to Texinfo.
1323 CONTENTS is nil. INFO is a plist holding contextual information."
1324 (org-element-property :value statistics-cookie))
1326 ;;;; Subscript
1328 (defun org-texinfo-subscript (_subscript contents _info)
1329 "Transcode a SUBSCRIPT object from Org to Texinfo.
1330 CONTENTS is the contents of the object. INFO is a plist holding
1331 contextual information."
1332 (format "@math{_%s}" contents))
1334 ;;;; Superscript
1336 (defun org-texinfo-superscript (_superscript contents _info)
1337 "Transcode a SUPERSCRIPT object from Org to Texinfo.
1338 CONTENTS is the contents of the object. INFO is a plist holding
1339 contextual information."
1340 (format "@math{^%s}" contents))
1342 ;;;; Table
1344 (defun org-texinfo-table (table contents info)
1345 "Transcode a TABLE element from Org to Texinfo.
1346 CONTENTS is the contents of the table. INFO is a plist holding
1347 contextual information."
1348 (if (eq (org-element-property :type table) 'table.el)
1349 (format "@verbatim\n%s@end verbatim"
1350 (org-element-normalize-string
1351 (org-element-property :value table)))
1352 (let* ((col-width (org-export-read-attribute :attr_texinfo table :columns))
1353 (columns
1354 (if col-width (format "@columnfractions %s" col-width)
1355 (org-texinfo-table-column-widths table info)))
1356 (caption (org-export-get-caption table))
1357 (shortcaption (org-export-get-caption table t))
1358 (table-str (format "@multitable %s\n%s@end multitable"
1359 columns
1360 contents)))
1361 (if (not (or caption shortcaption)) table-str
1362 (org-texinfo--wrap-float table-str
1363 info
1364 (org-export-translate "Table" :utf-8 info)
1365 (org-export-get-reference table info)
1366 caption
1367 shortcaption)))))
1369 (defun org-texinfo-table-column-widths (table info)
1370 "Determine the largest table cell in each column to process alignment.
1371 TABLE is the table element to transcode. INFO is a plist used as
1372 a communication channel."
1373 (let ((widths (make-vector (cdr (org-export-table-dimensions table info)) 0)))
1374 (org-element-map table 'table-row
1375 (lambda (row)
1376 (let ((idx 0))
1377 (org-element-map row 'table-cell
1378 (lambda (cell)
1379 ;; Length of the cell in the original buffer is only an
1380 ;; approximation of the length of the cell in the
1381 ;; output. It can sometimes fail (e.g. it considers
1382 ;; "/a/" being larger than "ab").
1383 (let ((w (- (org-element-property :contents-end cell)
1384 (org-element-property :contents-begin cell))))
1385 (aset widths idx (max w (aref widths idx))))
1386 (incf idx))
1387 info)))
1388 info)
1389 (format "{%s}" (mapconcat (lambda (w) (make-string w ?a)) widths "} {"))))
1391 ;;;; Table Cell
1393 (defun org-texinfo-table-cell (table-cell contents info)
1394 "Transcode a TABLE-CELL element from Org to Texinfo.
1395 CONTENTS is the cell contents. INFO is a plist used as
1396 a communication channel."
1397 (concat
1398 (let ((scientific-notation
1399 (plist-get info :texinfo-table-scientific-notation)))
1400 (if (and contents
1401 scientific-notation
1402 (string-match orgtbl-exp-regexp contents))
1403 ;; Use appropriate format string for scientific notation.
1404 (format scientific-notation
1405 (match-string 1 contents)
1406 (match-string 2 contents))
1407 contents))
1408 (when (org-export-get-next-element table-cell info) "\n@tab ")))
1410 ;;;; Table Row
1412 (defun org-texinfo-table-row (table-row contents info)
1413 "Transcode a TABLE-ROW element from Org to Texinfo.
1414 CONTENTS is the contents of the row. INFO is a plist used as
1415 a communication channel."
1416 ;; Rules are ignored since table separators are deduced from
1417 ;; borders of the current row.
1418 (when (eq (org-element-property :type table-row) 'standard)
1419 (let ((rowgroup-tag
1420 (if (and (= 1 (org-export-table-row-group table-row info))
1421 (org-export-table-has-header-p
1422 (org-export-get-parent-table table-row) info))
1423 "@headitem "
1424 "@item ")))
1425 (concat rowgroup-tag contents "\n"))))
1427 ;;;; Target
1429 (defun org-texinfo-target (target _contents info)
1430 "Transcode a TARGET object from Org to Texinfo.
1431 CONTENTS is nil. INFO is a plist holding contextual
1432 information."
1433 (format "@anchor{%s}" (org-export-get-reference target info)))
1435 ;;;; Timestamp
1437 (defun org-texinfo-timestamp (timestamp _contents info)
1438 "Transcode a TIMESTAMP object from Org to Texinfo.
1439 CONTENTS is nil. INFO is a plist holding contextual
1440 information."
1441 (let ((value (org-texinfo-plain-text
1442 (org-timestamp-translate timestamp) info)))
1443 (case (org-element-property :type timestamp)
1444 ((active active-range)
1445 (format (plist-get info :texinfo-active-timestamp-format) value))
1446 ((inactive inactive-range)
1447 (format (plist-get info :texinfo-inactive-timestamp-format) value))
1448 (t (format (plist-get info :texinfo-diary-timestamp-format) value)))))
1450 ;;;; Verbatim
1452 (defun org-texinfo-verbatim (verbatim _contents info)
1453 "Transcode a VERBATIM object from Org to Texinfo.
1454 CONTENTS is nil. INFO is a plist used as a communication
1455 channel."
1456 (org-texinfo--text-markup
1457 (org-element-property :value verbatim) 'verbatim info))
1459 ;;;; Verse Block
1461 (defun org-texinfo-verse-block (_verse-block contents _info)
1462 "Transcode a VERSE-BLOCK element from Org to Texinfo.
1463 CONTENTS is verse block contents. INFO is a plist holding
1464 contextual information."
1465 (format "@display\n%s@end display" contents))
1468 ;;; Interactive functions
1470 (defun org-texinfo-export-to-texinfo
1471 (&optional async subtreep visible-only body-only ext-plist)
1472 "Export current buffer to a Texinfo file.
1474 If narrowing is active in the current buffer, only export its
1475 narrowed part.
1477 If a region is active, export that region.
1479 A non-nil optional argument ASYNC means the process should happen
1480 asynchronously. The resulting file should be accessible through
1481 the `org-export-stack' interface.
1483 When optional argument SUBTREEP is non-nil, export the sub-tree
1484 at point, extracting information from the headline properties
1485 first.
1487 When optional argument VISIBLE-ONLY is non-nil, don't export
1488 contents of hidden elements.
1490 When optional argument BODY-ONLY is non-nil, only write code
1491 between \"\\begin{document}\" and \"\\end{document}\".
1493 EXT-PLIST, when provided, is a property list with external
1494 parameters overriding Org default settings, but still inferior to
1495 file-local settings.
1497 Return output file's name."
1498 (interactive)
1499 (let ((outfile (org-export-output-file-name ".texi" subtreep))
1500 (org-export-coding-system org-texinfo-coding-system))
1501 (org-export-to-file 'texinfo outfile
1502 async subtreep visible-only body-only ext-plist)))
1504 (defun org-texinfo-export-to-info
1505 (&optional async subtreep visible-only body-only ext-plist)
1506 "Export current buffer to Texinfo then process through to INFO.
1508 If narrowing is active in the current buffer, only export its
1509 narrowed part.
1511 If a region is active, export that region.
1513 A non-nil optional argument ASYNC means the process should happen
1514 asynchronously. The resulting file should be accessible through
1515 the `org-export-stack' interface.
1517 When optional argument SUBTREEP is non-nil, export the sub-tree
1518 at point, extracting information from the headline properties
1519 first.
1521 When optional argument VISIBLE-ONLY is non-nil, don't export
1522 contents of hidden elements.
1524 When optional argument BODY-ONLY is non-nil, only write code
1525 between \"\\begin{document}\" and \"\\end{document}\".
1527 EXT-PLIST, when provided, is a property list with external
1528 parameters overriding Org default settings, but still inferior to
1529 file-local settings.
1531 When optional argument PUB-DIR is set, use it as the publishing
1532 directory.
1534 Return INFO file's name."
1535 (interactive)
1536 (let ((outfile (org-export-output-file-name ".texi" subtreep))
1537 (org-export-coding-system org-texinfo-coding-system))
1538 (org-export-to-file 'texinfo outfile
1539 async subtreep visible-only body-only ext-plist
1540 (lambda (file) (org-texinfo-compile file)))))
1542 ;;;###autoload
1543 (defun org-texinfo-publish-to-texinfo (plist filename pub-dir)
1544 "Publish an org file to Texinfo.
1546 FILENAME is the filename of the Org file to be published. PLIST
1547 is the property list for the given project. PUB-DIR is the
1548 publishing directory.
1550 Return output file name."
1551 (org-publish-org-to 'texinfo filename ".texi" plist pub-dir))
1553 ;;;###autoload
1554 (defun org-texinfo-convert-region-to-texinfo ()
1555 "Assume the current region has org-mode syntax, and convert it to Texinfo.
1556 This can be used in any buffer. For example, you can write an
1557 itemized list in org-mode syntax in an Texinfo buffer and use
1558 this command to convert it."
1559 (interactive)
1560 (org-export-replace-region-by 'texinfo))
1562 (defun org-texinfo-compile (file)
1563 "Compile a texinfo file.
1565 FILE is the name of the file being compiled. Processing is
1566 done through the command specified in `org-texinfo-info-process'.
1568 Return INFO file name or an error if it couldn't be produced."
1569 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1570 (full-name (file-truename file))
1571 (out-dir (file-name-directory file))
1572 ;; Properly set working directory for compilation.
1573 (default-directory (if (file-name-absolute-p file)
1574 (file-name-directory full-name)
1575 default-directory))
1576 errors)
1577 (message "Processing Texinfo file %s..." file)
1578 (save-window-excursion
1579 ;; Replace %b, %f and %o with appropriate values in each command
1580 ;; before applying it. Output is redirected to "*Org INFO
1581 ;; Texinfo Output*" buffer.
1582 (let ((outbuf (get-buffer-create "*Org INFO Texinfo Output*")))
1583 (with-current-buffer outbuf (compilation-mode))
1584 (dolist (command org-texinfo-info-process)
1585 (shell-command
1586 (replace-regexp-in-string
1587 "%b" (shell-quote-argument base-name)
1588 (replace-regexp-in-string
1589 "%f" (shell-quote-argument full-name)
1590 (replace-regexp-in-string
1591 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1592 outbuf))
1593 ;; Collect standard errors from output buffer.
1594 (setq errors (org-texinfo-collect-errors outbuf)))
1595 (let ((infofile (concat out-dir base-name ".info")))
1596 ;; Check for process failure. Provide collected errors if
1597 ;; possible.
1598 (if (not (file-exists-p infofile))
1599 (error "INFO file %s wasn't produced%s" infofile
1600 (if errors (concat ": " errors) ""))
1601 ;; Else remove log files, when specified, and signal end of
1602 ;; process to user, along with any error encountered.
1603 (when org-texinfo-remove-logfiles
1604 (dolist (ext org-texinfo-logfiles-extensions)
1605 (let ((file (concat out-dir base-name "." ext)))
1606 (when (file-exists-p file) (delete-file file)))))
1607 (message (concat "Process completed"
1608 (if (not errors) "."
1609 (concat " with errors: " errors)))))
1610 ;; Return output file name.
1611 infofile))))
1613 (defun org-texinfo-collect-errors (buffer)
1614 "Collect some kind of errors from \"makeinfo\" command output.
1616 BUFFER is the buffer containing output.
1618 Return collected error types as a string, or nil if there was
1619 none."
1620 (with-current-buffer buffer
1621 (save-excursion
1622 (goto-char (point-min))
1623 ;; Find final "makeinfo" run.
1624 (when t
1625 (let ((case-fold-search t)
1626 (errors ""))
1627 (when (save-excursion
1628 (re-search-forward "perhaps incorrect sectioning?" nil t))
1629 (setq errors (concat errors " [incorrect sectioning]")))
1630 (when (save-excursion
1631 (re-search-forward "missing close brace" nil t))
1632 (setq errors (concat errors " [syntax error]")))
1633 (when (save-excursion
1634 (re-search-forward "Unknown command" nil t))
1635 (setq errors (concat errors " [undefined @command]")))
1636 (when (save-excursion
1637 (re-search-forward "No matching @end" nil t))
1638 (setq errors (concat errors " [block incomplete]")))
1639 (when (save-excursion
1640 (re-search-forward "requires a sectioning" nil t))
1641 (setq errors (concat errors " [invalid section command]")))
1642 (when (save-excursion
1643 (re-search-forward "\\[unexpected\ ]" nil t))
1644 (setq errors (concat errors " [unexpected error]")))
1645 (when (save-excursion
1646 (re-search-forward "misplaced " nil t))
1647 (setq errors (concat errors " [syntax error]")))
1648 (and (org-string-nw-p errors) (org-trim errors)))))))
1651 (provide 'ox-texinfo)
1653 ;; Local variables:
1654 ;; generated-autoload-file: "org-loaddefs.el"
1655 ;; End:
1657 ;;; ox-texinfo.el ends here