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