ox-texinfo: Improve entities handling
[org-mode.git] / lisp / ox-texinfo.el
blobe01ee5a9afabc0065612cb4fc08bdf8334e43c8e
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.
273 The format should have \"%s\" twice, for mantissa and exponent
274 \(i.e. \"%s\\\\times10^{%s}\").
276 When nil, no transformation is made."
277 :group 'org-export-texinfo
278 :type '(choice
279 (string :tag "Format string")
280 (const :tag "No formatting" nil)))
282 (defcustom org-texinfo-def-table-markup "@samp"
283 "Default markup for first column in two-column tables.
285 This should an indicating command, e.g., \"@code\", \"@kbd\" or
286 \"@asis\".
288 It can be overridden locally using the \":indic\" attribute."
289 :group 'org-export-texinfo
290 :type 'string)
292 ;;;; Text markup
294 (defcustom org-texinfo-text-markup-alist '((bold . "@strong{%s}")
295 (code . code)
296 (italic . "@emph{%s}")
297 (verbatim . verb))
298 "Alist of Texinfo expressions to convert text markup.
300 The key must be a symbol among `bold', `code', `italic',
301 `strike-through', `underscore' and `verbatim'. The value is
302 a formatting string to wrap fontified text with.
304 Value can also be set to the following symbols: `verb' and
305 `code'. For the former, Org will use \"@verb\" to create
306 a format string and select a delimiter character that isn't in
307 the string. For the latter, Org will use \"@code\" to typeset
308 and try to protect special characters.
310 If no association can be found for a given markup, text will be
311 returned as-is."
312 :group 'org-export-texinfo
313 :type 'alist
314 :options '(bold code italic strike-through underscore verbatim))
316 ;;;; Drawers
318 (defcustom org-texinfo-format-drawer-function (lambda (_name contents) contents)
319 "Function called to format a drawer in Texinfo code.
321 The function must accept two parameters:
322 NAME the drawer name, like \"LOGBOOK\"
323 CONTENTS the contents of the drawer.
325 The function should return the string to be exported.
327 The default function simply returns the value of CONTENTS."
328 :group 'org-export-texinfo
329 :version "24.4"
330 :package-version '(Org . "8.2")
331 :type 'function)
333 ;;;; Inlinetasks
335 (defcustom org-texinfo-format-inlinetask-function
336 'org-texinfo-format-inlinetask-default-function
337 "Function called to format an inlinetask in Texinfo code.
339 The function must accept six parameters:
340 TODO the todo keyword, as a string
341 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
342 PRIORITY the inlinetask priority, as a string
343 NAME the inlinetask name, as a string.
344 TAGS the inlinetask tags, as a list of strings.
345 CONTENTS the contents of the inlinetask, as a string.
347 The function should return the string to be exported."
348 :group 'org-export-texinfo
349 :type 'function)
351 ;;;; Compilation
353 (defcustom org-texinfo-info-process '("makeinfo %f")
354 "Commands to process a Texinfo file to an INFO file.
356 This is a list of strings, each of them will be given to the
357 shell as a command. %f in the command will be replaced by the
358 relative file name, %F by the absolute file name, %b by the file
359 base name (i.e. without directory and extension parts), %o by the
360 base directory of the file and %O by the absolute file name of
361 the output file."
362 :group 'org-export-texinfo
363 :type '(repeat :tag "Shell command sequence"
364 (string :tag "Shell command")))
366 (defcustom org-texinfo-logfiles-extensions
367 '("aux" "toc" "cp" "fn" "ky" "pg" "tp" "vr")
368 "The list of file extensions to consider as Texinfo logfiles.
369 The logfiles will be remove if `org-texinfo-remove-logfiles' is
370 non-nil."
371 :group 'org-export-texinfo
372 :type '(repeat (string :tag "Extension")))
374 (defcustom org-texinfo-remove-logfiles t
375 "Non-nil means remove the logfiles produced by compiling a Texinfo file.
376 By default, logfiles are files with these extensions: .aux, .toc,
377 .cp, .fn, .ky, .pg and .tp. To define the set of logfiles to remove,
378 set `org-texinfo-logfiles-extensions'."
379 :group 'org-export-latex
380 :type 'boolean)
382 ;;; Constants
384 (defconst org-texinfo-max-toc-depth 4
385 "Maximum depth for creation of detailed menu listings.
386 Beyond this depth, Texinfo will not recognize the nodes and will
387 cause errors. Left as a constant in case this value ever
388 changes.")
390 (defconst org-texinfo-supported-coding-systems
391 '("US-ASCII" "UTF-8" "ISO-8859-15" "ISO-8859-1" "ISO-8859-2" "koi8-r" "koi8-u")
392 "List of coding systems supported by Texinfo, as strings.
393 Specified coding system will be matched against these strings.
394 If two strings share the same prefix (e.g. \"ISO-8859-1\" and
395 \"ISO-8859-15\"), the most specific one has to be listed first.")
397 (defconst org-texinfo-inline-image-rules
398 (list (cons "file"
399 (regexp-opt '("eps" "pdf" "png" "jpg" "jpeg" "gif" "svg"))))
400 "Rules characterizing image files that can be inlined.")
403 ;;; Internal Functions
405 (defun org-texinfo--filter-section-blank-lines (headline _backend _info)
406 "Filter controlling number of blank lines after a section."
407 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" "\n\n" headline))
409 (defun org-texinfo--normalize-headlines (tree _backend info)
410 "Normalize headlines in TREE.
412 BACK-END is the symbol specifying back-end used for export. INFO
413 is a plist used as a communication channel.
415 Make sure every headline in TREE contains a section, since those
416 are required to install a menu. Also put exactly one blank line
417 at the end of each section.
419 Return new tree."
420 (org-element-map tree 'headline
421 (lambda (hl)
422 (org-element-put-property hl :post-blank 1)
423 (let ((contents (org-element-contents hl)))
424 (when contents
425 (let ((first (org-element-map contents '(headline section)
426 #'identity info t)))
427 (unless (eq (org-element-type first) 'section)
428 (apply #'org-element-set-contents
430 (cons `(section (:parent ,hl)) contents)))))))
431 info)
432 tree)
434 (defun org-texinfo--find-verb-separator (s)
435 "Return a character not used in string S.
436 This is used to choose a separator for constructs like \\verb."
437 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
438 (cl-loop for c across ll
439 when (not (string-match (regexp-quote (char-to-string c)) s))
440 return (char-to-string c))))
442 (defun org-texinfo--text-markup (text markup _info)
443 "Format TEXT depending on MARKUP text markup.
444 INFO is a plist used as a communication channel. See
445 `org-texinfo-text-markup-alist' for details."
446 (pcase (cdr (assq markup org-texinfo-text-markup-alist))
447 ;; No format string: Return raw text.
448 (`nil text)
449 (`verb
450 (let ((separator (org-texinfo--find-verb-separator text)))
451 (concat "@verb{" separator text separator "}")))
452 (`code
453 (format "@code{%s}" (replace-regexp-in-string "[@{}]" "@\\&" text)))
454 ;; Else use format string.
455 (fmt (format fmt text))))
457 (defun org-texinfo--get-node (datum info)
458 "Return node or anchor associated to DATUM.
459 DATUM is an element or object. INFO is a plist used as
460 a communication channel. The function guarantees the node or
461 anchor name is unique."
462 (let ((cache (plist-get info :texinfo-node-cache)))
463 (or (cdr (assq datum cache))
464 (let* ((salt 0)
465 (basename
466 (org-texinfo--sanitize-node
467 (if (eq (org-element-type datum) 'headline)
468 (org-texinfo--sanitize-title
469 (org-export-get-alt-title datum info) info)
470 (org-export-get-reference datum info))))
471 (name basename))
472 ;; Ensure NAME is unique and not reserved node name "Top".
473 (while (or (equal name "Top") (rassoc name cache))
474 (setq name (concat basename (format " %d" (cl-incf salt)))))
475 (plist-put info :texinfo-node-cache (cons (cons datum name) cache))
476 name))))
478 (defun org-texinfo--sanitize-node (title)
479 "Bend string TITLE to node line requirements.
480 Trim string and collapse multiple whitespace characters as they
481 are not significant. Replace leading left parenthesis, when
482 followed by a right parenthesis, with a square bracket. Remove
483 periods, commas and colons."
484 (org-trim
485 (replace-regexp-in-string
486 "[ \t]+" " "
487 (replace-regexp-in-string
488 "[:,.]" ""
489 (replace-regexp-in-string "\\`(\\(.*?)\\)" "[\\1" title)))))
491 (defun org-texinfo--sanitize-title (title info)
492 "Make TITLE suitable as a section name.
493 TITLE is a string or a secondary string. INFO is the current
494 export state, as a plist."
495 (org-export-data-with-backend
496 title
497 (org-export-create-backend
498 :parent 'texinfo
499 :transcoders '((footnote-reference . ignore)
500 (link . (lambda (object c i) c))
501 (radio-target . (lambda (object c i) c))
502 (target . ignore)))
503 info))
505 (defun org-texinfo--sanitize-content (text)
506 "Escape special characters in string TEXT.
507 Special characters are: @ { }"
508 (replace-regexp-in-string "[@{}]" "@\\&" text))
510 (defun org-texinfo--wrap-float (value info &optional type label caption short)
511 "Wrap string VALUE within a @float command.
512 INFO is the current export state, as a plist. TYPE is float
513 type, as a string. LABEL is the cross reference label for the
514 float, as a string. CAPTION and SHORT are, respectively, the
515 caption and shortcaption used for the float, as secondary
516 strings (e.g., returned by `org-export-get-caption')."
517 (let* ((backend
518 (org-export-create-backend
519 :parent 'texinfo
520 :transcoders '((link . (lambda (object c i) c))
521 (radio-target . (lambda (object c i) c))
522 (target . ignore))))
523 (short-backend
524 (org-export-create-backend
525 :parent 'texinfo
526 :transcoders '((footnote-reference . ignore)
527 (inline-src-block . ignore)
528 (link . (lambda (object c i) c))
529 (radio-target . (lambda (object c i) c))
530 (target . ignore)
531 (verbatim . ignore))))
532 (short-str
533 (if (and short caption)
534 (format "@shortcaption{%s}\n"
535 (org-export-data-with-backend short short-backend info))
536 ""))
537 (caption-str
538 (if (or short caption)
539 (format "@caption{%s}\n"
540 (org-export-data-with-backend
541 (or caption short)
542 (if (equal short-str "") short-backend backend)
543 info))
544 "")))
545 (format "@float %s%s\n%s\n%s%s@end float"
546 type (if label (concat "," label) "") value caption-str short-str)))
548 ;;; Template
550 (defun org-texinfo-template (contents info)
551 "Return complete document string after Texinfo conversion.
552 CONTENTS is the transcoded contents string. INFO is a plist
553 holding export options."
554 (let ((title (org-export-data (plist-get info :title) info))
555 ;; Copying data is the contents of the first headline in
556 ;; parse tree with a non-nil copying property.
557 (copying (org-element-map (plist-get info :parse-tree) 'headline
558 (lambda (hl)
559 (and (org-not-nil (org-element-property :COPYING hl))
560 (org-element-contents hl)))
561 info t)))
562 (concat
563 "\\input texinfo @c -*- texinfo -*-\n"
564 "@c %**start of header\n"
565 (let ((file (or (plist-get info :texinfo-filename)
566 (let ((f (plist-get info :output-file)))
567 (and f (concat (file-name-sans-extension f) ".info"))))))
568 (and file (format "@setfilename %s\n" file)))
569 (format "@settitle %s\n" title)
570 ;; Insert class-defined header.
571 (org-element-normalize-string
572 (let ((header (nth 1 (assoc (plist-get info :texinfo-class)
573 org-texinfo-classes)))
574 (coding
575 (catch 'coding-system
576 (let ((case-fold-search t)
577 (name (symbol-name (or org-texinfo-coding-system
578 buffer-file-coding-system))))
579 (dolist (system org-texinfo-supported-coding-systems "UTF-8")
580 (when (string-match-p (regexp-quote system) name)
581 (throw 'coding-system system))))))
582 (language (plist-get info :language))
583 (case-fold-search nil))
584 ;; Auto coding system.
585 (replace-regexp-in-string
586 "^@documentencoding \\(AUTO\\)$"
587 coding
588 (replace-regexp-in-string
589 "^@documentlanguage \\(AUTO\\)$" language header t nil 1) t nil 1)))
590 ;; Additional header options set by #+TEXINFO_HEADER.
591 (let ((texinfo-header (plist-get info :texinfo-header)))
592 (and texinfo-header (org-element-normalize-string texinfo-header)))
593 "@c %**end of header\n\n"
594 ;; Additional options set by #+TEXINFO_POST_HEADER.
595 (let ((texinfo-post-header (plist-get info :texinfo-post-header)))
596 (and texinfo-post-header
597 (org-element-normalize-string texinfo-post-header)))
598 ;; Copying.
599 (and copying
600 (format "@copying\n%s@end copying\n\n"
601 (org-element-normalize-string
602 (org-export-data copying info))))
603 ;; Info directory information. Only supply if both title and
604 ;; category are provided.
605 (let ((dircat (plist-get info :texinfo-dircat))
606 (dirtitle
607 (let ((title (plist-get info :texinfo-dirtitle)))
608 (and title
609 (string-match "^\\(?:\\* \\)?\\(.*?\\)\\(\\.\\)?$" title)
610 (format "* %s." (match-string 1 title))))))
611 (when (and dircat dirtitle)
612 (concat "@dircategory " dircat "\n"
613 "@direntry\n"
614 (let ((dirdesc
615 (let ((desc (plist-get info :texinfo-dirdesc)))
616 (cond ((not desc) nil)
617 ((string-suffix-p "." desc) desc)
618 (t (concat desc "."))))))
619 (if dirdesc (format "%-23s %s" dirtitle dirdesc) dirtitle))
620 "\n"
621 "@end direntry\n\n")))
622 ;; Title
623 "@finalout\n"
624 "@titlepage\n"
625 (when (plist-get info :with-title)
626 (concat
627 (format "@title %s\n" (or (plist-get info :texinfo-printed-title) title ""))
628 (let ((subtitle (plist-get info :subtitle)))
629 (when subtitle
630 (format "@subtitle %s\n"
631 (org-export-data subtitle info))))))
632 (when (plist-get info :with-author)
633 (concat
634 ;; Primary author.
635 (let ((author (org-string-nw-p
636 (org-export-data (plist-get info :author) info)))
637 (email (and (plist-get info :with-email)
638 (org-string-nw-p
639 (org-export-data (plist-get info :email) info)))))
640 (cond ((and author email)
641 (format "@author %s (@email{%s})\n" author email))
642 (author (format "@author %s\n" author))
643 (email (format "@author @email{%s}\n" email))))
644 ;; Other authors.
645 (let ((subauthor (plist-get info :subauthor)))
646 (and subauthor
647 (org-element-normalize-string
648 (replace-regexp-in-string "^" "@author " subauthor))))))
649 (and copying "@page\n@vskip 0pt plus 1filll\n@insertcopying\n")
650 "@end titlepage\n\n"
651 ;; Table of contents.
652 (and (plist-get info :with-toc) "@contents\n\n")
653 ;; Configure Top Node when not for Tex
654 "@ifnottex\n"
655 "@node Top\n"
656 (format "@top %s\n" title)
657 (and copying "@insertcopying\n")
658 "@end ifnottex\n\n"
659 ;; Menu.
660 (org-texinfo-make-menu (plist-get info :parse-tree) info 'master)
661 "\n"
662 ;; Document's body.
663 contents "\n"
664 ;; Creator.
665 (and (plist-get info :with-creator)
666 (concat (plist-get info :creator) "\n"))
667 ;; Document end.
668 "@bye")))
672 ;;; Transcode Functions
674 ;;;; Bold
676 (defun org-texinfo-bold (_bold contents info)
677 "Transcode BOLD from Org to Texinfo.
678 CONTENTS is the text with bold markup. INFO is a plist holding
679 contextual information."
680 (org-texinfo--text-markup contents 'bold info))
682 ;;;; Center Block
684 (defun org-texinfo-center-block (_center-block contents _info)
685 "Transcode a CENTER-BLOCK element from Org to Texinfo.
686 CONTENTS holds the contents of the block. INFO is a plist used
687 as a communication channel."
688 contents)
690 ;;;; Clock
692 (defun org-texinfo-clock (clock _contents info)
693 "Transcode a CLOCK element from Org to Texinfo.
694 CONTENTS is nil. INFO is a plist holding contextual
695 information."
696 (concat
697 "@noindent"
698 (format "@strong{%s} " org-clock-string)
699 (format (plist-get info :texinfo-inactive-timestamp-format)
700 (concat (org-timestamp-translate (org-element-property :value clock))
701 (let ((time (org-element-property :duration clock)))
702 (and time (format " (%s)" time)))))
703 "@*"))
705 ;;;; Code
707 (defun org-texinfo-code (code _contents info)
708 "Transcode a CODE object from Org to Texinfo.
709 CONTENTS is nil. INFO is a plist used as a communication
710 channel."
711 (org-texinfo--text-markup (org-element-property :value code) 'code info))
713 ;;;; Drawer
715 (defun org-texinfo-drawer (drawer contents info)
716 "Transcode a DRAWER element from Org to Texinfo.
717 CONTENTS holds the contents of the block. INFO is a plist
718 holding contextual information."
719 (let* ((name (org-element-property :drawer-name drawer))
720 (output (funcall (plist-get info :texinfo-format-drawer-function)
721 name contents)))
722 output))
724 ;;;; Dynamic Block
726 (defun org-texinfo-dynamic-block (_dynamic-block contents _info)
727 "Transcode a DYNAMIC-BLOCK element from Org to Texinfo.
728 CONTENTS holds the contents of the block. INFO is a plist
729 holding contextual information."
730 contents)
732 ;;;; Entity
734 (defun org-texinfo-entity (entity _contents _info)
735 "Transcode an ENTITY object from Org to Texinfo."
736 ;; Since there is not specific Texinfo entry in entities, use
737 ;; Texinfo-specific commands whenever possible, and fallback to
738 ;; UTF-8 otherwise.
739 (pcase (org-element-property :name entity)
740 ("AElig" "@AE{}")
741 ("aelig" "@ae{}")
742 ((or "bull" "bullet") "@bullet{}")
743 ("copy" "@copyright{}")
744 ((or "dots" "hellip") "@dots{}")
745 ("equiv" "@equiv{}")
746 ((or "euro" "EUR") "@euro{}")
747 ((or "ge" "geq") "@geq{}")
748 ("laquo" "@guillemetleft{}")
749 ("iexcl" "@exclamdown{}")
750 ("imath" "@dotless{i}")
751 ("iquest" "@questiondown{}")
752 ("jmath" "@dotless{j}")
753 ((or "le" "leq") "@leq{}")
754 ("lsaquo" "@guilsinglleft{}")
755 ("mdash" "---")
756 ("minus" "@minus{}")
757 ("nbsp" "@tie{}")
758 ("ndash" "--")
759 ("OElig" "@OE{}")
760 ("oelig" "@oe{}")
761 ("ordf" "@ordf{}")
762 ("ordm" "@ordm{}")
763 ("pound" "@pound{}")
764 ("raquo" "@guillemetright{}")
765 ((or "rArr" "Rightarrow") "@result{}")
766 ((or "rightarrow" "to" "rarr") "@arrow{}")
767 ("rsaquo" "@guilsinglright{}")
768 ("thorn" "@th{}")
769 ("THORN" "@TH{}")
770 ((and (pred (string-prefix-p "_")) name) ;spacing entities.
771 (format "@w{%s}" (substring name 1)))
772 (_ (org-element-property :utf-8 entity))))
774 ;;;; Example Block
776 (defun org-texinfo-example-block (example-block _contents info)
777 "Transcode an EXAMPLE-BLOCK element from Org to Texinfo.
778 CONTENTS is nil. INFO is a plist holding contextual
779 information."
780 (format "@verbatim\n%s@end verbatim"
781 (org-export-format-code-default example-block info)))
783 ;;; Export Block
785 (defun org-texinfo-export-block (export-block _contents _info)
786 "Transcode a EXPORT-BLOCK element from Org to Texinfo.
787 CONTENTS is nil. INFO is a plist holding contextual information."
788 (when (string= (org-element-property :type export-block) "TEXINFO")
789 (org-remove-indentation (org-element-property :value export-block))))
791 ;;; Export Snippet
793 (defun org-texinfo-export-snippet (export-snippet _contents _info)
794 "Transcode a EXPORT-SNIPPET object from Org to Texinfo.
795 CONTENTS is nil. INFO is a plist holding contextual information."
796 (when (eq (org-export-snippet-backend export-snippet) 'texinfo)
797 (org-element-property :value export-snippet)))
799 ;;;; Fixed Width
801 (defun org-texinfo-fixed-width (fixed-width _contents _info)
802 "Transcode a FIXED-WIDTH element from Org to Texinfo.
803 CONTENTS is nil. INFO is a plist holding contextual information."
804 (format "@example\n%s@end example"
805 (org-remove-indentation
806 (org-texinfo--sanitize-content
807 (org-element-property :value fixed-width)))))
809 ;;;; Footnote Reference
811 (defun org-texinfo-footnote-reference (footnote _contents info)
812 "Create a footnote reference for FOOTNOTE.
814 FOOTNOTE is the footnote to define. CONTENTS is nil. INFO is a
815 plist holding contextual information."
816 (let ((def (org-export-get-footnote-definition footnote info)))
817 (format "@footnote{%s}"
818 (org-trim (org-export-data def info)))))
820 ;;;; Headline
822 (defun org-texinfo-headline (headline contents info)
823 "Transcode a HEADLINE element from Org to Texinfo.
824 CONTENTS holds the contents of the headline. INFO is a plist
825 holding contextual information."
826 (let* ((class (plist-get info :texinfo-class))
827 (level (org-export-get-relative-level headline info))
828 (numberedp (org-export-numbered-headline-p headline info))
829 (class-sectioning (assoc class (plist-get info :texinfo-classes)))
830 ;; Find the index type, if any.
831 (index (org-element-property :INDEX headline))
832 ;; Create node info, to insert it before section formatting.
833 ;; Use custom menu title if present.
834 (node (format "@node %s\n" (org-texinfo--get-node headline info)))
835 ;; Section formatting will set two placeholders: one for the
836 ;; title and the other for the contents.
837 (section-fmt
838 (if (org-not-nil (org-element-property :APPENDIX headline))
839 "@appendix %s\n%s"
840 (let ((sec (if (and (symbolp (nth 2 class-sectioning))
841 (fboundp (nth 2 class-sectioning)))
842 (funcall (nth 2 class-sectioning) level numberedp)
843 (nth (1+ level) class-sectioning))))
844 (cond
845 ;; No section available for that LEVEL.
846 ((not sec) nil)
847 ;; Section format directly returned by a function.
848 ((stringp sec) sec)
849 ;; (numbered-section . unnumbered-section)
850 ((not (consp (cdr sec)))
851 (concat (if (or index (not numberedp)) (cdr sec) (car sec))
852 "\n%s"))))))
853 (todo
854 (and (plist-get info :with-todo-keywords)
855 (let ((todo (org-element-property :todo-keyword headline)))
856 (and todo (org-export-data todo info)))))
857 (todo-type (and todo (org-element-property :todo-type headline)))
858 (tags (and (plist-get info :with-tags)
859 (org-export-get-tags headline info)))
860 (priority (and (plist-get info :with-priority)
861 (org-element-property :priority headline)))
862 (text (org-texinfo--sanitize-title
863 (org-element-property :title headline) info))
864 (full-text (funcall (plist-get info :texinfo-format-headline-function)
865 todo todo-type priority text tags))
866 (contents (if (org-string-nw-p contents) (concat "\n" contents) "")))
867 (cond
868 ;; Case 1: This is a footnote section: ignore it.
869 ((org-element-property :footnote-section-p headline) nil)
870 ;; Case 2: This is the `copying' section: ignore it
871 ;; This is used elsewhere.
872 ((org-not-nil (org-element-property :COPYING headline)) nil)
873 ;; Case 3: An index. If it matches one of the known indexes,
874 ;; print it as such following the contents, otherwise
875 ;; print the contents and leave the index up to the user.
876 (index
877 (concat node
878 (format
879 section-fmt
880 full-text
881 (concat contents
882 (and (member index '("cp" "fn" "ky" "pg" "tp" "vr"))
883 (concat "\n@printindex " index))))))
884 ;; Case 4: This is a deep sub-tree: export it as a list item.
885 ;; Also export as items headlines for which no section
886 ;; format has been found.
887 ((or (not section-fmt) (org-export-low-level-p headline info))
888 ;; Build the real contents of the sub-tree.
889 (concat (and (org-export-first-sibling-p headline info)
890 (format "@%s\n" (if numberedp 'enumerate 'itemize)))
891 "@item\n" full-text "\n"
892 contents
893 (if (org-export-last-sibling-p headline info)
894 (format "@end %s" (if numberedp 'enumerate 'itemize))
895 "\n")))
896 ;; Case 5: Standard headline. Export it as a section.
897 (t (concat node (format section-fmt full-text contents))))))
899 (defun org-texinfo-format-headline-default-function
900 (todo _todo-type priority text tags)
901 "Default format function for a headline.
902 See `org-texinfo-format-headline-function' for details."
903 (concat (when todo (format "@strong{%s} " todo))
904 (when priority (format "@emph{#%s} " priority))
905 text
906 (when tags (format " :%s:" (mapconcat 'identity tags ":")))))
908 ;;;; Inline Src Block
910 (defun org-texinfo-inline-src-block (inline-src-block _contents _info)
911 "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
912 CONTENTS holds the contents of the item. INFO is a plist holding
913 contextual information."
914 (let* ((code (org-element-property :value inline-src-block))
915 (separator (org-texinfo--find-verb-separator code)))
916 (concat "@verb{" separator code separator "}")))
918 ;;;; Inlinetask
920 (defun org-texinfo-inlinetask (inlinetask contents info)
921 "Transcode an INLINETASK element from Org to Texinfo.
922 CONTENTS holds the contents of the block. INFO is a plist
923 holding contextual information."
924 (let ((title (org-export-data (org-element-property :title inlinetask) info))
925 (todo (and (plist-get info :with-todo-keywords)
926 (let ((todo (org-element-property :todo-keyword inlinetask)))
927 (and todo (org-export-data todo info)))))
928 (todo-type (org-element-property :todo-type inlinetask))
929 (tags (and (plist-get info :with-tags)
930 (org-export-get-tags inlinetask info)))
931 (priority (and (plist-get info :with-priority)
932 (org-element-property :priority inlinetask))))
933 (funcall (plist-get info :texinfo-format-inlinetask-function)
934 todo todo-type priority title tags contents)))
936 (defun org-texinfo-format-inlinetask-default-function
937 (todo _todo-type priority title tags contents)
938 "Default format function for a inlinetasks.
939 See `org-texinfo-format-inlinetask-function' for details."
940 (let ((full-title
941 (concat (when todo (format "@strong{%s} " todo))
942 (when priority (format "#%c " priority))
943 title
944 (when tags (format ":%s:" (mapconcat #'identity tags ":"))))))
945 (format "@center %s\n\n%s\n" full-title contents)))
947 ;;;; Italic
949 (defun org-texinfo-italic (_italic contents info)
950 "Transcode ITALIC from Org to Texinfo.
951 CONTENTS is the text with italic markup. INFO is a plist holding
952 contextual information."
953 (org-texinfo--text-markup contents 'italic info))
955 ;;;; Item
957 (defun org-texinfo-item (item contents info)
958 "Transcode an ITEM element from Org to Texinfo.
959 CONTENTS holds the contents of the item. INFO is a plist holding
960 contextual information."
961 (format "@item%s\n%s"
962 (let ((tag (org-element-property :tag item)))
963 (if tag (concat " " (org-export-data tag info)) ""))
964 (or contents "")))
966 ;;;; Keyword
968 (defun org-texinfo-keyword (keyword _contents info)
969 "Transcode a KEYWORD element from Org to Texinfo.
970 CONTENTS is nil. INFO is a plist holding contextual information."
971 (let ((key (org-element-property :key keyword))
972 (value (org-element-property :value keyword)))
973 (cond
974 ((string= key "TEXINFO") value)
975 ((string= key "CINDEX") (format "@cindex %s" value))
976 ((string= key "FINDEX") (format "@findex %s" value))
977 ((string= key "KINDEX") (format "@kindex %s" value))
978 ((string= key "PINDEX") (format "@pindex %s" value))
979 ((string= key "TINDEX") (format "@tindex %s" value))
980 ((string= key "VINDEX") (format "@vindex %s" value))
981 ((string= key "TOC")
982 (cond ((string-match-p "\\<tables\\>" value)
983 (concat "@listoffloats "
984 (org-export-translate "Table" :utf-8 info)))
985 ((string-match-p "\\<listings\\>" value)
986 (concat "@listoffloats "
987 (org-export-translate "Listing" :utf-8 info))))))))
989 ;;;; Line Break
991 (defun org-texinfo-line-break (_line-break _contents _info)
992 "Transcode a LINE-BREAK object from Org to Texinfo.
993 CONTENTS is nil. INFO is a plist holding contextual information."
994 "@*\n")
996 ;;;; Link
998 (defun org-texinfo--@ref (datum description info)
999 "Return @ref command for element or object DATUM.
1000 DESCRIPTION is the name of the section to print, as a string."
1001 (let ((node-name (org-texinfo--get-node datum info))
1002 ;; Sanitize DESCRIPTION for cross-reference use. In
1003 ;; particular, remove colons as they seem to cause (even
1004 ;; within @asis{...} to the Texinfo reader.
1005 (title (replace-regexp-in-string
1006 "[ \t]*:+" ""
1007 (replace-regexp-in-string "," "@comma{}" description))))
1008 (if (equal title node-name)
1009 (format "@ref{%s}" node-name)
1010 (format "@ref{%s, , %s}" node-name title))))
1012 (defun org-texinfo-link (link desc info)
1013 "Transcode a LINK object from Org to Texinfo.
1014 DESC is the description part of the link, or the empty string.
1015 INFO is a plist holding contextual information. See
1016 `org-export-data'."
1017 (let* ((type (org-element-property :type link))
1018 (raw-path (org-element-property :path link))
1019 ;; Ensure DESC really exists, or set it to nil.
1020 (desc (and (not (string= desc "")) desc))
1021 (path (cond
1022 ((member type '("http" "https" "ftp"))
1023 (concat type ":" raw-path))
1024 ((string= type "file") (org-export-file-uri raw-path))
1025 (t raw-path))))
1026 (cond
1027 ((org-export-custom-protocol-maybe link desc 'texinfo))
1028 ((org-export-inline-image-p link org-texinfo-inline-image-rules)
1029 (org-texinfo--inline-image link info))
1030 ((equal type "radio")
1031 (let ((destination (org-export-resolve-radio-link link info)))
1032 (if (not destination) desc
1033 (org-texinfo--@ref destination desc info))))
1034 ((member type '("custom-id" "id" "fuzzy"))
1035 (let ((destination
1036 (if (equal type "fuzzy")
1037 (org-export-resolve-fuzzy-link link info)
1038 (org-export-resolve-id-link link info))))
1039 (pcase (org-element-type destination)
1040 (`nil
1041 (format org-texinfo-link-with-unknown-path-format
1042 (org-texinfo--sanitize-content path)))
1043 ;; Id link points to an external file.
1044 (`plain-text
1045 (if desc (format "@uref{file://%s,%s}" destination desc)
1046 (format "@uref{file://%s}" destination)))
1047 ((or `headline
1048 ;; Targets within headlines cannot be turned into
1049 ;; @anchor{}, so we refer to the headline parent
1050 ;; directly.
1051 (and `target
1052 (guard (eq 'headline
1053 (org-element-type
1054 (org-element-property :parent destination))))))
1055 (let ((headline (org-element-lineage destination '(headline) t)))
1056 (org-texinfo--@ref
1057 headline
1058 (or desc (org-texinfo--sanitize-title
1059 (org-element-property :title headline) info))
1060 info)))
1062 (org-texinfo--@ref
1063 destination
1064 (or desc
1065 (pcase (org-export-get-ordinal destination info)
1066 ((and (pred integerp) n) (number-to-string n))
1067 ((and (pred consp) n) (mapconcat #'number-to-string n "."))
1068 (_ "???")))
1069 info))))) ;cannot guess the description
1070 ((equal type "info")
1071 (let* ((info-path (split-string path "[:#]"))
1072 (info-manual (car info-path))
1073 (info-node (or (cadr info-path) "Top"))
1074 (title (or desc "")))
1075 (format "@ref{%s,%s,,%s,}" info-node title info-manual)))
1076 ((string= type "mailto")
1077 (format "@email{%s}"
1078 (concat (org-texinfo--sanitize-content path)
1079 (and desc (concat ", " desc)))))
1080 ;; External link with a description part.
1081 ((and path desc) (format "@uref{%s, %s}" path desc))
1082 ;; External link without a description part.
1083 (path (format "@uref{%s}" path))
1084 ;; No path, only description. Try to do something useful.
1086 (format (plist-get info :texinfo-link-with-unknown-path-format) desc)))))
1088 (defun org-texinfo--inline-image (link info)
1089 "Return Texinfo code for an inline image.
1090 LINK is the link pointing to the inline image. INFO is the
1091 current state of the export, as a plist."
1092 (let* ((parent (org-export-get-parent-element link))
1093 (label (and (org-element-property :name parent)
1094 (org-texinfo--get-node parent info)))
1095 (caption (org-export-get-caption parent))
1096 (shortcaption (org-export-get-caption parent t))
1097 (path (org-element-property :path link))
1098 (filename
1099 (file-name-sans-extension
1100 (if (file-name-absolute-p path) (expand-file-name path) path)))
1101 (extension (file-name-extension path))
1102 (attributes (org-export-read-attribute :attr_texinfo parent))
1103 (height (or (plist-get attributes :height) ""))
1104 (width (or (plist-get attributes :width) ""))
1105 (alt (or (plist-get attributes :alt) ""))
1106 (image (format "@image{%s,%s,%s,%s,%s}"
1107 filename width height alt extension)))
1108 (cond ((or caption shortcaption)
1109 (org-texinfo--wrap-float image
1110 info
1111 (org-export-translate "Figure" :utf-8 info)
1112 label
1113 caption
1114 shortcaption))
1115 (label (concat "@anchor{" label "}\n" image))
1116 (t image))))
1119 ;;;; Menu
1121 (defun org-texinfo-make-menu (scope info &optional master)
1122 "Create the menu for inclusion in the Texinfo document.
1124 SCOPE is a headline or a full parse tree. INFO is the
1125 communication channel, as a plist.
1127 When optional argument MASTER is non-nil, generate a master menu,
1128 including detailed node listing."
1129 (let ((menu (org-texinfo--build-menu scope info)))
1130 (when (org-string-nw-p menu)
1131 (org-element-normalize-string
1132 (format
1133 "@menu\n%s@end menu"
1134 (concat menu
1135 (when master
1136 (let ((detailmenu
1137 (org-texinfo--build-menu
1138 scope info
1139 (let ((toc-depth (plist-get info :with-toc)))
1140 (if (wholenump toc-depth) toc-depth
1141 org-texinfo-max-toc-depth)))))
1142 (when (org-string-nw-p detailmenu)
1143 (concat "\n@detailmenu\n"
1144 "--- The Detailed Node Listing ---\n\n"
1145 detailmenu
1146 "@end detailmenu\n"))))))))))
1148 (defun org-texinfo--build-menu (scope info &optional level)
1149 "Build menu for entries within SCOPE.
1150 SCOPE is a headline or a full parse tree. INFO is a plist
1151 containing contextual information. When optional argument LEVEL
1152 is an integer, build the menu recursively, down to this depth."
1153 (cond
1154 ((not level)
1155 (org-texinfo--format-entries (org-texinfo--menu-entries scope info) info))
1156 ((zerop level) nil)
1158 (org-element-normalize-string
1159 (mapconcat
1160 (lambda (h)
1161 (let ((entries (org-texinfo--menu-entries h info)))
1162 (when entries
1163 (concat
1164 (format "%s\n\n%s\n"
1165 (org-export-data (org-export-get-alt-title h info) info)
1166 (org-texinfo--format-entries entries info))
1167 (org-texinfo--build-menu h info (1- level))))))
1168 (org-texinfo--menu-entries scope info) "\n")))))
1170 (defun org-texinfo--format-entries (entries info)
1171 "Format all direct menu entries in SCOPE, as a string.
1172 SCOPE is either a headline or a full Org document. INFO is
1173 a plist containing contextual information."
1174 (org-element-normalize-string
1175 (mapconcat
1176 (lambda (h)
1177 (let* ((title
1178 ;; Colons are used as a separator between title and node
1179 ;; name. Remove them.
1180 (replace-regexp-in-string
1181 "[ \t]+:+" ""
1182 (org-texinfo--sanitize-title
1183 (org-export-get-alt-title h info) info)))
1184 (node (org-texinfo--get-node h info))
1185 (entry (concat "* " title ":"
1186 (if (string= title node) ":"
1187 (concat " " node ". "))))
1188 (desc (org-element-property :DESCRIPTION h)))
1189 (if (not desc) entry
1190 (format (format "%%-%ds %%s" org-texinfo-node-description-column)
1191 entry desc))))
1192 entries "\n")))
1194 (defun org-texinfo--menu-entries (scope info)
1195 "List direct children in SCOPE needing a menu entry.
1196 SCOPE is a headline or a full parse tree. INFO is a plist
1197 holding contextual information."
1198 (let* ((cache (or (plist-get info :texinfo-entries-cache)
1199 (plist-get (plist-put info :texinfo-entries-cache
1200 (make-hash-table :test #'eq))
1201 :texinfo-entries-cache)))
1202 (cached-entries (gethash scope cache 'no-cache)))
1203 (if (not (eq cached-entries 'no-cache)) cached-entries
1204 (puthash scope
1205 (org-element-map (org-element-contents scope) 'headline
1206 (lambda (h)
1207 (and (not (org-not-nil (org-element-property :COPYING h)))
1208 (not (org-element-property :footnote-section-p h))
1209 (not (org-export-low-level-p h info))
1211 info nil 'headline)
1212 cache))))
1214 ;;;; Node Property
1216 (defun org-texinfo-node-property (node-property _contents _info)
1217 "Transcode a NODE-PROPERTY element from Org to Texinfo.
1218 CONTENTS is nil. INFO is a plist holding contextual
1219 information."
1220 (format "%s:%s"
1221 (org-element-property :key node-property)
1222 (let ((value (org-element-property :value node-property)))
1223 (if value (concat " " value) ""))))
1225 ;;;; Paragraph
1227 (defun org-texinfo-paragraph (_paragraph contents _info)
1228 "Transcode a PARAGRAPH element from Org to Texinfo.
1229 CONTENTS is the contents of the paragraph, as a string. INFO is
1230 the plist used as a communication channel."
1231 contents)
1233 ;;;; Plain List
1235 (defun org-texinfo-plain-list (plain-list contents info)
1236 "Transcode a PLAIN-LIST element from Org to Texinfo.
1237 CONTENTS is the contents of the list. INFO is a plist holding
1238 contextual information."
1239 (let* ((attr (org-export-read-attribute :attr_texinfo plain-list))
1240 (indic (let ((i (or (plist-get attr :indic)
1241 (plist-get info :texinfo-def-table-markup))))
1242 ;; Allow indicating commands with missing @ sign.
1243 (if (string-prefix-p "@" i) i (concat "@" i))))
1244 (table-type (plist-get attr :table-type))
1245 (type (org-element-property :type plain-list))
1246 (list-type (cond
1247 ((eq type 'ordered) "enumerate")
1248 ((eq type 'unordered) "itemize")
1249 ((member table-type '("ftable" "vtable")) table-type)
1250 (t "table"))))
1251 (format "@%s\n%s@end %s"
1252 (if (eq type 'descriptive) (concat list-type " " indic) list-type)
1253 contents
1254 list-type)))
1256 ;;;; Plain Text
1258 (defun org-texinfo-plain-text (text info)
1259 "Transcode a TEXT string from Org to Texinfo.
1260 TEXT is the string to transcode. INFO is a plist holding
1261 contextual information."
1262 ;; First protect @, { and }.
1263 (let ((output (org-texinfo--sanitize-content text)))
1264 ;; Activate smart quotes. Be sure to provide original TEXT string
1265 ;; since OUTPUT may have been modified.
1266 (when (plist-get info :with-smart-quotes)
1267 (setq output
1268 (org-export-activate-smart-quotes output :texinfo info text)))
1269 ;; LaTeX into @LaTeX{} and TeX into @TeX{}
1270 (let ((case-fold-search nil))
1271 (setq output (replace-regexp-in-string "\\(?:La\\)?TeX" "@\\&{}" output)))
1272 ;; Convert special strings.
1273 (when (plist-get info :with-special-strings)
1274 (setq output
1275 (replace-regexp-in-string
1276 "\\.\\.\\." "@dots{}"
1277 (replace-regexp-in-string "\\\\-" "@-" output))))
1278 ;; Handle break preservation if required.
1279 (when (plist-get info :preserve-breaks)
1280 (setq output (replace-regexp-in-string
1281 "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" output)))
1282 ;; Return value.
1283 output))
1285 ;;;; Planning
1287 (defun org-texinfo-planning (planning _contents info)
1288 "Transcode a PLANNING element from Org to Texinfo.
1289 CONTENTS is nil. INFO is a plist holding contextual
1290 information."
1291 (concat
1292 "@noindent"
1293 (mapconcat
1294 'identity
1295 (delq nil
1296 (list
1297 (let ((closed (org-element-property :closed planning)))
1298 (when closed
1299 (concat
1300 (format "@strong{%s} " org-closed-string)
1301 (format (plist-get info :texinfo-inactive-timestamp-format)
1302 (org-timestamp-translate closed)))))
1303 (let ((deadline (org-element-property :deadline planning)))
1304 (when deadline
1305 (concat
1306 (format "@strong{%s} " org-deadline-string)
1307 (format (plist-get info :texinfo-active-timestamp-format)
1308 (org-timestamp-translate deadline)))))
1309 (let ((scheduled (org-element-property :scheduled planning)))
1310 (when scheduled
1311 (concat
1312 (format "@strong{%s} " org-scheduled-string)
1313 (format (plist-get info :texinfo-active-timestamp-format)
1314 (org-timestamp-translate scheduled)))))))
1315 " ")
1316 "@*"))
1318 ;;;; Property Drawer
1320 (defun org-texinfo-property-drawer (_property-drawer contents _info)
1321 "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
1322 CONTENTS holds the contents of the drawer. INFO is a plist
1323 holding contextual information."
1324 (and (org-string-nw-p contents)
1325 (format "@verbatim\n%s@end verbatim" contents)))
1327 ;;;; Quote Block
1329 (defun org-texinfo-quote-block (quote-block contents _info)
1330 "Transcode a QUOTE-BLOCK element from Org to Texinfo.
1331 CONTENTS holds the contents of the block. INFO is a plist
1332 holding contextual information."
1333 (let* ((title (org-element-property :name quote-block))
1334 (start-quote (concat "@quotation"
1335 (if title
1336 (format " %s" title)))))
1337 (format "%s\n%s@end quotation" start-quote contents)))
1339 ;;;; Radio Target
1341 (defun org-texinfo-radio-target (radio-target text info)
1342 "Transcode a RADIO-TARGET object from Org to Texinfo.
1343 TEXT is the text of the target. INFO is a plist holding
1344 contextual information."
1345 (format "@anchor{%s}%s"
1346 (org-texinfo--get-node radio-target info)
1347 text))
1349 ;;;; Section
1351 (defun org-texinfo-section (section contents info)
1352 "Transcode a SECTION element from Org to Texinfo.
1353 CONTENTS holds the contents of the section. INFO is a plist
1354 holding contextual information."
1355 (org-trim
1356 (concat contents
1357 "\n"
1358 (let ((parent (org-export-get-parent-headline section)))
1359 (and parent (org-texinfo-make-menu parent info))))))
1361 ;;;; Special Block
1363 (defun org-texinfo-special-block (special-block contents _info)
1364 "Transcode a SPECIAL-BLOCK element from Org to Texinfo.
1365 CONTENTS holds the contents of the block. INFO is a plist used
1366 as a communication channel."
1367 (let ((opt (org-export-read-attribute :attr_texinfo special-block :options))
1368 (type (org-element-property :type special-block)))
1369 (format "@%s%s\n%s@end %s"
1370 type
1371 (if opt (concat " " opt) opt)
1372 (or contents "")
1373 type)))
1375 ;;;; Src Block
1377 (defun org-texinfo-src-block (src-block _contents info)
1378 "Transcode a SRC-BLOCK element from Org to Texinfo.
1379 CONTENTS holds the contents of the item. INFO is a plist holding
1380 contextual information."
1381 (let* ((lisp (string-match-p "lisp"
1382 (org-element-property :language src-block)))
1383 (code (org-texinfo--sanitize-content
1384 (org-export-format-code-default src-block info)))
1385 (value (format
1386 (if lisp "@lisp\n%s@end lisp" "@example\n%s@end example")
1387 code))
1388 (caption (org-export-get-caption src-block))
1389 (shortcaption (org-export-get-caption src-block t)))
1390 (if (not (or caption shortcaption)) value
1391 (org-texinfo--wrap-float value
1392 info
1393 (org-export-translate "Listing" :utf-8 info)
1394 (org-texinfo--get-node src-block info)
1395 caption
1396 shortcaption))))
1398 ;;;; Statistics Cookie
1400 (defun org-texinfo-statistics-cookie (statistics-cookie _contents _info)
1401 "Transcode a STATISTICS-COOKIE object from Org to Texinfo.
1402 CONTENTS is nil. INFO is a plist holding contextual information."
1403 (org-element-property :value statistics-cookie))
1406 ;;;; Strike-through
1408 (defun org-texinfo-strike-through (_strike-through contents info)
1409 "Transcode STRIKE-THROUGH from Org to Texinfo.
1410 CONTENTS is the text with strike-through markup. INFO is a plist
1411 holding contextual information."
1412 (org-texinfo--text-markup contents 'strike-through info))
1414 ;;;; Subscript
1416 (defun org-texinfo-subscript (_subscript contents _info)
1417 "Transcode a SUBSCRIPT object from Org to Texinfo.
1418 CONTENTS is the contents of the object. INFO is a plist holding
1419 contextual information."
1420 (format "@math{_%s}" contents))
1422 ;;;; Superscript
1424 (defun org-texinfo-superscript (_superscript contents _info)
1425 "Transcode a SUPERSCRIPT object from Org to Texinfo.
1426 CONTENTS is the contents of the object. INFO is a plist holding
1427 contextual information."
1428 (format "@math{^%s}" contents))
1430 ;;;; Table
1432 (defun org-texinfo-table (table contents info)
1433 "Transcode a TABLE element from Org to Texinfo.
1434 CONTENTS is the contents of the table. INFO is a plist holding
1435 contextual information."
1436 (if (eq (org-element-property :type table) 'table.el)
1437 (format "@verbatim\n%s@end verbatim"
1438 (org-element-normalize-string
1439 (org-element-property :value table)))
1440 (let* ((col-width (org-export-read-attribute :attr_texinfo table :columns))
1441 (columns
1442 (if col-width (format "@columnfractions %s" col-width)
1443 (org-texinfo-table-column-widths table info)))
1444 (caption (org-export-get-caption table))
1445 (shortcaption (org-export-get-caption table t))
1446 (table-str (format "@multitable %s\n%s@end multitable"
1447 columns
1448 contents)))
1449 (if (not (or caption shortcaption)) table-str
1450 (org-texinfo--wrap-float table-str
1451 info
1452 (org-export-translate "Table" :utf-8 info)
1453 (org-texinfo--get-node table info)
1454 caption
1455 shortcaption)))))
1457 (defun org-texinfo-table-column-widths (table info)
1458 "Determine the largest table cell in each column to process alignment.
1459 TABLE is the table element to transcode. INFO is a plist used as
1460 a communication channel."
1461 (let ((widths (make-vector (cdr (org-export-table-dimensions table info)) 0)))
1462 (org-element-map table 'table-row
1463 (lambda (row)
1464 (let ((idx 0))
1465 (org-element-map row 'table-cell
1466 (lambda (cell)
1467 ;; Length of the cell in the original buffer is only an
1468 ;; approximation of the length of the cell in the
1469 ;; output. It can sometimes fail (e.g. it considers
1470 ;; "/a/" being larger than "ab").
1471 (let ((w (- (org-element-property :contents-end cell)
1472 (org-element-property :contents-begin cell))))
1473 (aset widths idx (max w (aref widths idx))))
1474 (cl-incf idx))
1475 info)))
1476 info)
1477 (format "{%s}" (mapconcat (lambda (w) (make-string w ?a)) widths "} {"))))
1479 ;;;; Table Cell
1481 (defun org-texinfo-table-cell (table-cell contents info)
1482 "Transcode a TABLE-CELL element from Org to Texinfo.
1483 CONTENTS is the cell contents. INFO is a plist used as
1484 a communication channel."
1485 (concat
1486 (let ((scientific-notation
1487 (plist-get info :texinfo-table-scientific-notation)))
1488 (if (and contents
1489 scientific-notation
1490 (string-match orgtbl-exp-regexp contents))
1491 ;; Use appropriate format string for scientific notation.
1492 (format scientific-notation
1493 (match-string 1 contents)
1494 (match-string 2 contents))
1495 contents))
1496 (when (org-export-get-next-element table-cell info) "\n@tab ")))
1498 ;;;; Table Row
1500 (defun org-texinfo-table-row (table-row contents info)
1501 "Transcode a TABLE-ROW element from Org to Texinfo.
1502 CONTENTS is the contents of the row. INFO is a plist used as
1503 a communication channel."
1504 ;; Rules are ignored since table separators are deduced from
1505 ;; borders of the current row.
1506 (when (eq (org-element-property :type table-row) 'standard)
1507 (let ((rowgroup-tag
1508 (if (and (= 1 (org-export-table-row-group table-row info))
1509 (org-export-table-has-header-p
1510 (org-export-get-parent-table table-row) info))
1511 "@headitem "
1512 "@item ")))
1513 (concat rowgroup-tag contents "\n"))))
1515 ;;;; Target
1517 (defun org-texinfo-target (target _contents info)
1518 "Transcode a TARGET object from Org to Texinfo.
1519 CONTENTS is nil. INFO is a plist holding contextual
1520 information."
1521 (format "@anchor{%s}" (org-texinfo--get-node target info)))
1523 ;;;; Timestamp
1525 (defun org-texinfo-timestamp (timestamp _contents info)
1526 "Transcode a TIMESTAMP object from Org to Texinfo.
1527 CONTENTS is nil. INFO is a plist holding contextual
1528 information."
1529 (let ((value (org-texinfo-plain-text
1530 (org-timestamp-translate timestamp) info)))
1531 (pcase (org-element-property :type timestamp)
1532 ((or `active `active-range)
1533 (format (plist-get info :texinfo-active-timestamp-format) value))
1534 ((or `inactive `inactive-range)
1535 (format (plist-get info :texinfo-inactive-timestamp-format) value))
1536 (_ (format (plist-get info :texinfo-diary-timestamp-format) value)))))
1538 ;;;; Underline
1540 (defun org-texinfo-underline (_underline contents info)
1541 "Transcode UNDERLINE from Org to Texinfo.
1542 CONTENTS is the text with underline markup. INFO is a plist
1543 holding contextual information."
1544 (org-texinfo--text-markup contents 'underline info))
1546 ;;;; Verbatim
1548 (defun org-texinfo-verbatim (verbatim _contents info)
1549 "Transcode a VERBATIM object from Org to Texinfo.
1550 CONTENTS is nil. INFO is a plist used as a communication
1551 channel."
1552 (org-texinfo--text-markup
1553 (org-element-property :value verbatim) 'verbatim info))
1555 ;;;; Verse Block
1557 (defun org-texinfo-verse-block (_verse-block contents _info)
1558 "Transcode a VERSE-BLOCK element from Org to Texinfo.
1559 CONTENTS is verse block contents. INFO is a plist holding
1560 contextual information."
1561 (format "@display\n%s@end display" contents))
1564 ;;; Interactive functions
1566 (defun org-texinfo-export-to-texinfo
1567 (&optional async subtreep visible-only body-only ext-plist)
1568 "Export current buffer to a Texinfo file.
1570 If narrowing is active in the current buffer, only export its
1571 narrowed part.
1573 If a region is active, export that region.
1575 A non-nil optional argument ASYNC means the process should happen
1576 asynchronously. The resulting file should be accessible through
1577 the `org-export-stack' interface.
1579 When optional argument SUBTREEP is non-nil, export the sub-tree
1580 at point, extracting information from the headline properties
1581 first.
1583 When optional argument VISIBLE-ONLY is non-nil, don't export
1584 contents of hidden elements.
1586 When optional argument BODY-ONLY is non-nil, only write code
1587 between \"\\begin{document}\" and \"\\end{document}\".
1589 EXT-PLIST, when provided, is a property list with external
1590 parameters overriding Org default settings, but still inferior to
1591 file-local settings.
1593 Return output file's name."
1594 (interactive)
1595 (let ((outfile (org-export-output-file-name ".texi" subtreep))
1596 (org-export-coding-system org-texinfo-coding-system))
1597 (org-export-to-file 'texinfo outfile
1598 async subtreep visible-only body-only ext-plist)))
1600 (defun org-texinfo-export-to-info
1601 (&optional async subtreep visible-only body-only ext-plist)
1602 "Export current buffer to Texinfo then process through to INFO.
1604 If narrowing is active in the current buffer, only export its
1605 narrowed part.
1607 If a region is active, export that region.
1609 A non-nil optional argument ASYNC means the process should happen
1610 asynchronously. The resulting file should be accessible through
1611 the `org-export-stack' interface.
1613 When optional argument SUBTREEP is non-nil, export the sub-tree
1614 at point, extracting information from the headline properties
1615 first.
1617 When optional argument VISIBLE-ONLY is non-nil, don't export
1618 contents of hidden elements.
1620 When optional argument BODY-ONLY is non-nil, only write code
1621 between \"\\begin{document}\" and \"\\end{document}\".
1623 EXT-PLIST, when provided, is a property list with external
1624 parameters overriding Org default settings, but still inferior to
1625 file-local settings.
1627 When optional argument PUB-DIR is set, use it as the publishing
1628 directory.
1630 Return INFO file's name."
1631 (interactive)
1632 (let ((outfile (org-export-output-file-name ".texi" subtreep))
1633 (org-export-coding-system org-texinfo-coding-system))
1634 (org-export-to-file 'texinfo outfile
1635 async subtreep visible-only body-only ext-plist
1636 (lambda (file) (org-texinfo-compile file)))))
1638 ;;;###autoload
1639 (defun org-texinfo-publish-to-texinfo (plist filename pub-dir)
1640 "Publish an org file to Texinfo.
1642 FILENAME is the filename of the Org file to be published. PLIST
1643 is the property list for the given project. PUB-DIR is the
1644 publishing directory.
1646 Return output file name."
1647 (org-publish-org-to 'texinfo filename ".texi" plist pub-dir))
1649 ;;;###autoload
1650 (defun org-texinfo-convert-region-to-texinfo ()
1651 "Assume the current region has Org syntax, and convert it to Texinfo.
1652 This can be used in any buffer. For example, you can write an
1653 itemized list in Org syntax in an Texinfo buffer and use this
1654 command to convert it."
1655 (interactive)
1656 (org-export-replace-region-by 'texinfo))
1658 (defun org-texinfo-compile (file)
1659 "Compile a texinfo file.
1661 FILE is the name of the file being compiled. Processing is done
1662 through the command specified in `org-texinfo-info-process',
1663 which see. Output is redirected to \"*Org INFO Texinfo Output*\"
1664 buffer.
1666 Return INFO file name or an error if it couldn't be produced."
1667 (message "Processing Texinfo file %s..." file)
1668 (let* ((log-name "*Org INFO Texinfo Output*")
1669 (log (get-buffer-create log-name))
1670 (output
1671 (org-compile-file file org-texinfo-info-process "info"
1672 (format "See %S for details" log-name)
1673 log)))
1674 (when org-texinfo-remove-logfiles
1675 (let ((base (file-name-sans-extension output)))
1676 (dolist (ext org-texinfo-logfiles-extensions)
1677 (let ((file (concat base "." ext)))
1678 (when (file-exists-p file) (delete-file file))))))
1679 (message "Process completed.")
1680 output))
1683 (provide 'ox-texinfo)
1685 ;; Local variables:
1686 ;; generated-autoload-file: "org-loaddefs.el"
1687 ;; End:
1689 ;;; ox-texinfo.el ends here