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