ox-texinfo: Preserve target name as node.
[org-mode.git] / lisp / ox-texinfo.el
blob66d3fc908a43f2c2684921e8c456926b9c830f45
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 <https://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 (:filter-final-output . org-texinfo--untabify))
88 :menu-entry
89 '(?i "Export to Texinfo"
90 ((?t "As TEXI file" org-texinfo-export-to-texinfo)
91 (?i "As INFO file" org-texinfo-export-to-info)
92 (?o "As INFO file and open"
93 (lambda (a s v b)
94 (if a (org-texinfo-export-to-info t s v b)
95 (org-open-file (org-texinfo-export-to-info nil s v b)))))))
96 :options-alist
97 '((:texinfo-filename "TEXINFO_FILENAME" nil nil t)
98 (:texinfo-class "TEXINFO_CLASS" nil org-texinfo-default-class t)
99 (:texinfo-header "TEXINFO_HEADER" nil nil newline)
100 (:texinfo-post-header "TEXINFO_POST_HEADER" nil nil newline)
101 (:subtitle "SUBTITLE" nil nil parse)
102 (:subauthor "SUBAUTHOR" nil nil newline)
103 (:texinfo-dircat "TEXINFO_DIR_CATEGORY" nil nil t)
104 (:texinfo-dirtitle "TEXINFO_DIR_TITLE" nil nil t)
105 (:texinfo-dirdesc "TEXINFO_DIR_DESC" nil nil t)
106 (:texinfo-printed-title "TEXINFO_PRINTED_TITLE" nil nil t)
107 ;; Other variables.
108 (:texinfo-classes nil nil org-texinfo-classes)
109 (:texinfo-format-headline-function nil nil org-texinfo-format-headline-function)
110 (:texinfo-node-description-column nil nil org-texinfo-node-description-column)
111 (:texinfo-active-timestamp-format nil nil org-texinfo-active-timestamp-format)
112 (:texinfo-inactive-timestamp-format nil nil org-texinfo-inactive-timestamp-format)
113 (:texinfo-diary-timestamp-format nil nil org-texinfo-diary-timestamp-format)
114 (:texinfo-link-with-unknown-path-format nil nil org-texinfo-link-with-unknown-path-format)
115 (:texinfo-tables-verbatim nil nil org-texinfo-tables-verbatim)
116 (:texinfo-table-scientific-notation nil nil org-texinfo-table-scientific-notation)
117 (:texinfo-table-default-markup nil nil org-texinfo-table-default-markup)
118 (:texinfo-text-markup-alist nil nil org-texinfo-text-markup-alist)
119 (:texinfo-format-drawer-function nil nil org-texinfo-format-drawer-function)
120 (:texinfo-format-inlinetask-function nil nil org-texinfo-format-inlinetask-function)))
124 ;;; User Configurable Variables
126 (defgroup org-export-texinfo nil
127 "Options for exporting Org mode files to Texinfo."
128 :tag "Org Export Texinfo"
129 :version "24.4"
130 :package-version '(Org . "8.0")
131 :group 'org-export)
133 ;;;; Preamble
135 (defcustom org-texinfo-coding-system nil
136 "Default document encoding for Texinfo output.
138 If nil it will default to `buffer-file-coding-system'."
139 :group 'org-export-texinfo
140 :type 'coding-system)
142 (defcustom org-texinfo-default-class "info"
143 "The default Texinfo class."
144 :group 'org-export-texinfo
145 :type '(string :tag "Texinfo class"))
147 (defcustom org-texinfo-classes
148 '(("info"
149 "@documentencoding AUTO\n@documentlanguage AUTO"
150 ("@chapter %s" "@unnumbered %s" "@chapheading %s" "@appendix %s")
151 ("@section %s" "@unnumberedsec %s" "@heading %s" "@appendixsec %s")
152 ("@subsection %s" "@unnumberedsubsec %s" "@subheading %s"
153 "@appendixsubsec %s")
154 ("@subsubsection %s" "@unnumberedsubsubsec %s" "@subsubheading %s"
155 "@appendixsubsubsec %s")))
156 "Alist of Texinfo classes and associated header and structure.
157 If #+TEXINFO_CLASS is set in the buffer, use its value and the
158 associated information. Here is the structure of a class
159 definition:
161 (class-name
162 header-string
163 (numbered-1 unnumbered-1 unnumbered-no-toc-1 appendix-1)
164 (numbered-2 unnumbered-2 unnumbered-no-toc-2 appendix-2)
165 ...)
168 The header string
169 -----------------
171 The header string is inserted in the header of the generated
172 document, right after \"@setfilename\" and \"@settitle\"
173 commands.
175 If it contains the special string
177 \"@documentencoding AUTO\"
179 \"AUTO\" will be replaced with an appropriate coding system. See
180 `org-texinfo-coding-system' for more information. Likewise, if
181 the string contains the special string
183 \"@documentlanguage AUTO\"
185 \"AUTO\" will be replaced with the language defined in the
186 buffer, through #+LANGUAGE keyword, or globally, with
187 `org-export-default-language', which see.
190 The sectioning structure
191 ------------------------
193 The sectioning structure of the class is given by the elements
194 following the header string. For each sectioning level, a number
195 of strings is specified. A %s formatter is mandatory in each
196 section string and will be replaced by the title of the section."
197 :group 'org-export-texinfo
198 :version "27.1"
199 :package-version '(Org . "9.2")
200 :type '(repeat
201 (list (string :tag "Texinfo class")
202 (string :tag "Texinfo header")
203 (repeat :tag "Levels" :inline t
204 (choice
205 (list :tag "Heading"
206 (string :tag " numbered")
207 (string :tag " unnumbered")
208 (string :tag "unnumbered-no-toc")
209 (string :tag " appendix")))))))
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-table-default-markup "@asis"
283 "Default markup for first column in two-column tables.
285 This should an indicating command, e.g., \"@code\", \"@kbd\" or
286 \"@samp\".
288 It can be overridden locally using the \":indic\" attribute."
289 :group 'org-export-texinfo
290 :type 'string
291 :version "26.1"
292 :package-version '(Org . "9.1")
293 :safe #'stringp)
295 ;;;; Text markup
297 (defcustom org-texinfo-text-markup-alist '((bold . "@strong{%s}")
298 (code . code)
299 (italic . "@emph{%s}")
300 (verbatim . samp))
301 "Alist of Texinfo expressions to convert text markup.
303 The key must be a symbol among `bold', `code', `italic',
304 `strike-through', `underscore' and `verbatim'. The value is
305 a formatting string to wrap fontified text with.
307 Value can also be set to the following symbols: `verb', `samp'
308 and `code'. With the first one, Org uses \"@verb\" to create
309 a format string and selects a delimiter character that isn't in
310 the string. For the other two, Org uses \"@samp\" or \"@code\"
311 to typeset and protects special characters.
313 When no association is found for a given markup, text is returned
314 as-is."
315 :group 'org-export-texinfo
316 :version "26.1"
317 :package-version '(Org . "9.1")
318 :type 'alist
319 :options '(bold code italic strike-through underscore verbatim))
321 ;;;; Drawers
323 (defcustom org-texinfo-format-drawer-function (lambda (_name contents) contents)
324 "Function called to format a drawer in Texinfo code.
326 The function must accept two parameters:
327 NAME the drawer name, like \"LOGBOOK\"
328 CONTENTS the contents of the drawer.
330 The function should return the string to be exported.
332 The default function simply returns the value of CONTENTS."
333 :group 'org-export-texinfo
334 :version "24.4"
335 :package-version '(Org . "8.2")
336 :type 'function)
338 ;;;; Inlinetasks
340 (defcustom org-texinfo-format-inlinetask-function
341 'org-texinfo-format-inlinetask-default-function
342 "Function called to format an inlinetask in Texinfo code.
344 The function must accept six parameters:
345 TODO the todo keyword, as a string
346 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
347 PRIORITY the inlinetask priority, as a string
348 NAME the inlinetask name, as a string.
349 TAGS the inlinetask tags, as a list of strings.
350 CONTENTS the contents of the inlinetask, as a string.
352 The function should return the string to be exported."
353 :group 'org-export-texinfo
354 :type 'function)
356 ;;;; Compilation
358 (defcustom org-texinfo-info-process '("makeinfo --no-split %f")
359 "Commands to process a Texinfo file to an INFO file.
361 This is a list of strings, each of them will be given to the
362 shell as a command. %f in the command will be replaced by the
363 relative file name, %F by the absolute file name, %b by the file
364 base name (i.e. without directory and extension parts), %o by the
365 base directory of the file and %O by the absolute file name of
366 the output file."
367 :group 'org-export-texinfo
368 :version "26.1"
369 :package-version '(Org . "9.1")
370 :type '(repeat :tag "Shell command sequence"
371 (string :tag "Shell command")))
373 (defcustom org-texinfo-logfiles-extensions
374 '("aux" "toc" "cp" "fn" "ky" "pg" "tp" "vr")
375 "The list of file extensions to consider as Texinfo logfiles.
376 The logfiles will be remove if `org-texinfo-remove-logfiles' is
377 non-nil."
378 :group 'org-export-texinfo
379 :type '(repeat (string :tag "Extension")))
381 (defcustom org-texinfo-remove-logfiles t
382 "Non-nil means remove the logfiles produced by compiling a Texinfo file.
383 By default, logfiles are files with these extensions: .aux, .toc,
384 .cp, .fn, .ky, .pg and .tp. To define the set of logfiles to remove,
385 set `org-texinfo-logfiles-extensions'."
386 :group 'org-export-latex
387 :type 'boolean)
389 ;;; Constants
391 (defconst org-texinfo-max-toc-depth 4
392 "Maximum depth for creation of detailed menu listings.
393 Beyond this depth, Texinfo will not recognize the nodes and will
394 cause errors. Left as a constant in case this value ever
395 changes.")
397 (defconst org-texinfo-supported-coding-systems
398 '("US-ASCII" "UTF-8" "ISO-8859-15" "ISO-8859-1" "ISO-8859-2" "koi8-r" "koi8-u")
399 "List of coding systems supported by Texinfo, as strings.
400 Specified coding system will be matched against these strings.
401 If two strings share the same prefix (e.g. \"ISO-8859-1\" and
402 \"ISO-8859-15\"), the most specific one has to be listed first.")
404 (defconst org-texinfo-inline-image-rules
405 (list (cons "file"
406 (regexp-opt '("eps" "pdf" "png" "jpg" "jpeg" "gif" "svg"))))
407 "Rules characterizing image files that can be inlined.")
410 ;;; Internal Functions
412 (defun org-texinfo--untabify (s _backend _info)
413 "Remove TAB characters in string S."
414 (replace-regexp-in-string "\t" (make-string tab-width ?\s) s))
416 (defun org-texinfo--filter-section-blank-lines (headline _backend _info)
417 "Filter controlling number of blank lines after a section."
418 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" "\n\n" headline))
420 (defun org-texinfo--normalize-headlines (tree _backend info)
421 "Normalize headlines in TREE.
423 BACK-END is the symbol specifying back-end used for export. INFO
424 is a plist used as a communication channel.
426 Make sure every headline in TREE contains a section, since those
427 are required to install a menu. Also put exactly one blank line
428 at the end of each section.
430 Return new tree."
431 (org-element-map tree 'headline
432 (lambda (hl)
433 (org-element-put-property hl :post-blank 1)
434 (let ((contents (org-element-contents hl)))
435 (when contents
436 (let ((first (org-element-map contents '(headline section)
437 #'identity info t)))
438 (unless (eq (org-element-type first) 'section)
439 (apply #'org-element-set-contents
441 (cons `(section (:parent ,hl)) contents)))))))
442 info)
443 tree)
445 (defun org-texinfo--find-verb-separator (s)
446 "Return a character not used in string S.
447 This is used to choose a separator for constructs like \\verb."
448 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
449 (cl-loop for c across ll
450 when (not (string-match (regexp-quote (char-to-string c)) s))
451 return (char-to-string c))))
453 (defun org-texinfo--text-markup (text markup _info)
454 "Format TEXT depending on MARKUP text markup.
455 INFO is a plist used as a communication channel. See
456 `org-texinfo-text-markup-alist' for details."
457 (pcase (cdr (assq markup org-texinfo-text-markup-alist))
458 (`nil text) ;no markup: return raw text
459 (`code (format "@code{%s}" (org-texinfo--sanitize-content text)))
460 (`samp (format "@samp{%s}" (org-texinfo--sanitize-content text)))
461 (`verb
462 (let ((separator (org-texinfo--find-verb-separator text)))
463 (format "@verb{%s%s%s}" separator text separator)))
464 ;; Else use format string.
465 (fmt (format fmt text))))
467 (defun org-texinfo--get-node (datum info)
468 "Return node or anchor associated to DATUM.
469 DATUM is a headline, a radio-target or a target. INFO is a plist
470 used as a communication channel. The function guarantees the
471 node or anchor name is unique."
472 (let ((cache (plist-get info :texinfo-node-cache)))
473 (or (cdr (assq datum cache))
474 (let* ((salt 0)
475 (basename
476 (org-texinfo--sanitize-node
477 (pcase (org-element-type datum)
478 (`headline
479 (org-texinfo--sanitize-title
480 (org-export-get-alt-title datum info) info))
481 (`radio-target
482 (org-texinfo--sanitize-title
483 (org-element-contents datum) info))
484 (`target
485 (org-export-data (org-element-property :value datum) info))
486 (type
487 (error "Cannot generate node name for type: %S" type)))))
488 (name basename))
489 ;; Ensure NAME is unique and not reserved node name "Top".
490 (while (or (equal name "Top") (rassoc name cache))
491 (setq name (concat basename (format " (%d)" (cl-incf salt)))))
492 (plist-put info :texinfo-node-cache (cons (cons datum name) cache))
493 name))))
495 (defun org-texinfo--sanitize-node (title)
496 "Bend string TITLE to node line requirements.
497 Trim string and collapse multiple whitespace characters as they
498 are not significant. Replace leading left parenthesis, when
499 followed by a right parenthesis, with a square bracket. Remove
500 periods, commas and colons."
501 (org-trim
502 (replace-regexp-in-string
503 "[ \t]+" " "
504 (replace-regexp-in-string
505 "[:,.]" ""
506 (replace-regexp-in-string "\\`(\\(.*?)\\)" "[\\1" title)))))
508 (defun org-texinfo--sanitize-title (title info)
509 "Make TITLE suitable as a section name.
510 TITLE is a string or a secondary string. INFO is the current
511 export state, as a plist."
512 (org-export-data-with-backend
513 title (org-export-toc-entry-backend 'texinfo) info))
515 (defun org-texinfo--sanitize-content (text)
516 "Escape special characters in string TEXT.
517 Special characters are: @ { }"
518 (replace-regexp-in-string "[@{}]" "@\\&" text))
520 (defun org-texinfo--wrap-float (value info &optional type label caption short)
521 "Wrap string VALUE within a @float command.
522 INFO is the current export state, as a plist. TYPE is float
523 type, as a string. LABEL is the cross reference label for the
524 float, as a string. CAPTION and SHORT are, respectively, the
525 caption and shortcaption used for the float, as secondary
526 strings (e.g., returned by `org-export-get-caption')."
527 (let* ((backend
528 (org-export-toc-entry-backend 'texinfo
529 (cons 'footnote-reference
530 (lambda (f c i) (org-export-with-backend 'texinfo f c i)))))
531 (short-backend
532 (org-export-toc-entry-backend 'texinfo
533 '(inline-src-block . ignore)
534 '(verbatim . ignore)))
535 (short-str
536 (if (and short caption)
537 (format "@shortcaption{%s}\n"
538 (org-export-data-with-backend short short-backend info))
539 ""))
540 (caption-str
541 (if (or short caption)
542 (format "@caption{%s}\n"
543 (org-export-data-with-backend
544 (or caption short)
545 (if (equal short-str "") short-backend backend)
546 info))
547 "")))
548 (format "@float %s%s\n%s\n%s%s@end float"
549 type (if label (concat "," label) "") value caption-str short-str)))
551 ;;; Template
553 (defun org-texinfo-template (contents info)
554 "Return complete document string after Texinfo conversion.
555 CONTENTS is the transcoded contents string. INFO is a plist
556 holding export options."
557 (let ((title (org-export-data (plist-get info :title) info))
558 ;; Copying data is the contents of the first headline in
559 ;; parse tree with a non-nil copying property.
560 (copying (org-element-map (plist-get info :parse-tree) 'headline
561 (lambda (hl)
562 (and (org-not-nil (org-element-property :COPYING hl))
563 (org-element-contents hl)))
564 info t)))
565 (concat
566 "\\input texinfo @c -*- texinfo -*-\n"
567 "@c %**start of header\n"
568 (let ((file (or (plist-get info :texinfo-filename)
569 (let ((f (plist-get info :output-file)))
570 (and f (concat (file-name-sans-extension f) ".info"))))))
571 (and file (format "@setfilename %s\n" file)))
572 (format "@settitle %s\n" title)
573 ;; Insert class-defined header.
574 (org-element-normalize-string
575 (let ((header (nth 1 (assoc (plist-get info :texinfo-class)
576 org-texinfo-classes)))
577 (coding
578 (catch 'coding-system
579 (let ((case-fold-search t)
580 (name (symbol-name (or org-texinfo-coding-system
581 buffer-file-coding-system))))
582 (dolist (system org-texinfo-supported-coding-systems "UTF-8")
583 (when (string-match-p (regexp-quote system) name)
584 (throw 'coding-system system))))))
585 (language (plist-get info :language))
586 (case-fold-search nil))
587 ;; Auto coding system.
588 (replace-regexp-in-string
589 "^@documentencoding \\(AUTO\\)$"
590 coding
591 (replace-regexp-in-string
592 "^@documentlanguage \\(AUTO\\)$" language header t nil 1) t nil 1)))
593 ;; Additional header options set by #+TEXINFO_HEADER.
594 (let ((texinfo-header (plist-get info :texinfo-header)))
595 (and texinfo-header (org-element-normalize-string texinfo-header)))
596 "@c %**end of header\n\n"
597 ;; Additional options set by #+TEXINFO_POST_HEADER.
598 (let ((texinfo-post-header (plist-get info :texinfo-post-header)))
599 (and texinfo-post-header
600 (org-element-normalize-string texinfo-post-header)))
601 ;; Copying.
602 (and copying
603 (format "@copying\n%s@end copying\n\n"
604 (org-element-normalize-string
605 (org-export-data copying info))))
606 ;; Info directory information. Only supply if both title and
607 ;; category are provided.
608 (let ((dircat (plist-get info :texinfo-dircat))
609 (dirtitle
610 (let ((title (plist-get info :texinfo-dirtitle)))
611 (and title
612 (string-match "^\\(?:\\* \\)?\\(.*?\\)\\(\\.\\)?$" title)
613 (format "* %s." (match-string 1 title))))))
614 (when (and dircat dirtitle)
615 (concat "@dircategory " dircat "\n"
616 "@direntry\n"
617 (let ((dirdesc
618 (let ((desc (plist-get info :texinfo-dirdesc)))
619 (cond ((not desc) nil)
620 ((string-suffix-p "." desc) desc)
621 (t (concat desc "."))))))
622 (if dirdesc (format "%-23s %s" dirtitle dirdesc) dirtitle))
623 "\n"
624 "@end direntry\n\n")))
625 ;; Title
626 "@finalout\n"
627 "@titlepage\n"
628 (when (plist-get info :with-title)
629 (concat
630 (format "@title %s\n"
631 (or (plist-get info :texinfo-printed-title) title ""))
632 (let ((subtitle (plist-get info :subtitle)))
633 (when subtitle
634 (format "@subtitle %s\n"
635 (org-export-data subtitle info))))))
636 (when (plist-get info :with-author)
637 (concat
638 ;; Primary author.
639 (let ((author (org-string-nw-p
640 (org-export-data (plist-get info :author) info)))
641 (email (and (plist-get info :with-email)
642 (org-string-nw-p
643 (org-export-data (plist-get info :email) info)))))
644 (cond ((and author email)
645 (format "@author %s (@email{%s})\n" author email))
646 (author (format "@author %s\n" author))
647 (email (format "@author @email{%s}\n" email))))
648 ;; Other authors.
649 (let ((subauthor (plist-get info :subauthor)))
650 (and subauthor
651 (org-element-normalize-string
652 (replace-regexp-in-string "^" "@author " subauthor))))))
653 (and copying "@page\n@vskip 0pt plus 1filll\n@insertcopying\n")
654 "@end titlepage\n\n"
655 ;; Table of contents.
656 (and (plist-get info :with-toc) "@contents\n\n")
657 ;; Configure Top Node when not for TeX. Also include contents
658 ;; from the first section of the document.
659 "@ifnottex\n"
660 "@node Top\n"
661 (format "@top %s\n" title)
662 (let* ((first-section
663 (org-element-map (plist-get info :parse-tree) 'section
664 #'identity info t '(headline)))
665 (top-contents
666 (org-export-data (org-element-contents first-section) info)))
667 (and (org-string-nw-p top-contents) (concat "\n" top-contents)))
668 "@end ifnottex\n\n"
669 ;; Menu.
670 (org-texinfo-make-menu (plist-get info :parse-tree) info 'master)
671 "\n"
672 ;; Document's body.
673 contents "\n"
674 ;; Creator.
675 (and (plist-get info :with-creator)
676 (concat (plist-get info :creator) "\n"))
677 ;; Document end.
678 "@bye")))
682 ;;; Transcode Functions
684 ;;;; Bold
686 (defun org-texinfo-bold (_bold contents info)
687 "Transcode BOLD from Org to Texinfo.
688 CONTENTS is the text with bold markup. INFO is a plist holding
689 contextual information."
690 (org-texinfo--text-markup contents 'bold info))
692 ;;;; Center Block
694 (defun org-texinfo-center-block (_center-block contents _info)
695 "Transcode a CENTER-BLOCK element from Org to Texinfo.
696 CONTENTS holds the contents of the block. INFO is a plist used
697 as a communication channel."
698 contents)
700 ;;;; Clock
702 (defun org-texinfo-clock (clock _contents info)
703 "Transcode a CLOCK element from Org to Texinfo.
704 CONTENTS is nil. INFO is a plist holding contextual
705 information."
706 (concat
707 "@noindent"
708 (format "@strong{%s} " org-clock-string)
709 (format (plist-get info :texinfo-inactive-timestamp-format)
710 (concat (org-timestamp-translate (org-element-property :value clock))
711 (let ((time (org-element-property :duration clock)))
712 (and time (format " (%s)" time)))))
713 "@*"))
715 ;;;; Code
717 (defun org-texinfo-code (code _contents info)
718 "Transcode a CODE object from Org to Texinfo.
719 CONTENTS is nil. INFO is a plist used as a communication
720 channel."
721 (org-texinfo--text-markup (org-element-property :value code) 'code info))
723 ;;;; Drawer
725 (defun org-texinfo-drawer (drawer contents info)
726 "Transcode a DRAWER element from Org to Texinfo.
727 CONTENTS holds the contents of the block. INFO is a plist
728 holding contextual information."
729 (let* ((name (org-element-property :drawer-name drawer))
730 (output (funcall (plist-get info :texinfo-format-drawer-function)
731 name contents)))
732 output))
734 ;;;; Dynamic Block
736 (defun org-texinfo-dynamic-block (_dynamic-block contents _info)
737 "Transcode a DYNAMIC-BLOCK element from Org to Texinfo.
738 CONTENTS holds the contents of the block. INFO is a plist
739 holding contextual information."
740 contents)
742 ;;;; Entity
744 (defun org-texinfo-entity (entity _contents _info)
745 "Transcode an ENTITY object from Org to Texinfo."
746 ;; Since there is not specific Texinfo entry in entities, use
747 ;; Texinfo-specific commands whenever possible, and fallback to
748 ;; UTF-8 otherwise.
749 (pcase (org-element-property :name entity)
750 ("AElig" "@AE{}")
751 ("aelig" "@ae{}")
752 ((or "bull" "bullet") "@bullet{}")
753 ("copy" "@copyright{}")
754 ("deg" "@textdegree{}")
755 ((or "dots" "hellip") "@dots{}")
756 ("equiv" "@equiv{}")
757 ((or "euro" "EUR") "@euro{}")
758 ((or "ge" "geq") "@geq{}")
759 ("laquo" "@guillemetleft{}")
760 ("iexcl" "@exclamdown{}")
761 ("imath" "@dotless{i}")
762 ("iquest" "@questiondown{}")
763 ("jmath" "@dotless{j}")
764 ((or "le" "leq") "@leq{}")
765 ("lsaquo" "@guilsinglleft{}")
766 ("mdash" "---")
767 ("minus" "@minus{}")
768 ("nbsp" "@tie{}")
769 ("ndash" "--")
770 ("OElig" "@OE{}")
771 ("oelig" "@oe{}")
772 ("ordf" "@ordf{}")
773 ("ordm" "@ordm{}")
774 ("pound" "@pound{}")
775 ("raquo" "@guillemetright{}")
776 ((or "rArr" "Rightarrow") "@result{}")
777 ("reg" "@registeredsymbol{}")
778 ((or "rightarrow" "to" "rarr") "@arrow{}")
779 ("rsaquo" "@guilsinglright{}")
780 ("thorn" "@th{}")
781 ("THORN" "@TH{}")
782 ((and (pred (string-prefix-p "_")) name) ;spacing entities
783 (format "@w{%s}" (substring name 1)))
784 (_ (org-element-property :utf-8 entity))))
786 ;;;; Example Block
788 (defun org-texinfo-example-block (example-block _contents info)
789 "Transcode an EXAMPLE-BLOCK element from Org to Texinfo.
790 CONTENTS is nil. INFO is a plist holding contextual
791 information."
792 (format "@example\n%s@end example"
793 (org-texinfo--sanitize-content
794 (org-export-format-code-default example-block info))))
796 ;;; Export Block
798 (defun org-texinfo-export-block (export-block _contents _info)
799 "Transcode a EXPORT-BLOCK element from Org to Texinfo.
800 CONTENTS is nil. INFO is a plist holding contextual information."
801 (when (string= (org-element-property :type export-block) "TEXINFO")
802 (org-remove-indentation (org-element-property :value export-block))))
804 ;;; Export Snippet
806 (defun org-texinfo-export-snippet (export-snippet _contents _info)
807 "Transcode a EXPORT-SNIPPET object from Org to Texinfo.
808 CONTENTS is nil. INFO is a plist holding contextual information."
809 (when (eq (org-export-snippet-backend export-snippet) 'texinfo)
810 (org-element-property :value export-snippet)))
812 ;;;; Fixed Width
814 (defun org-texinfo-fixed-width (fixed-width _contents _info)
815 "Transcode a FIXED-WIDTH element from Org to Texinfo.
816 CONTENTS is nil. INFO is a plist holding contextual information."
817 (format "@example\n%s@end example"
818 (org-remove-indentation
819 (org-texinfo--sanitize-content
820 (org-element-property :value fixed-width)))))
822 ;;;; Footnote Reference
824 (defun org-texinfo-footnote-reference (footnote _contents info)
825 "Create a footnote reference for FOOTNOTE.
827 FOOTNOTE is the footnote to define. CONTENTS is nil. INFO is a
828 plist holding contextual information."
829 (let ((def (org-export-get-footnote-definition footnote info)))
830 (format "@footnote{%s}"
831 (org-trim (org-export-data def info)))))
833 ;;;; Headline
835 (defun org-texinfo-headline (headline contents info)
836 "Transcode a HEADLINE element from Org to Texinfo.
837 CONTENTS holds the contents of the headline. INFO is a plist
838 holding 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)
843 (let* ((index (let ((i (org-export-get-node-property :INDEX headline t)))
844 (and (member i '("cp" "fn" "ky" "pg" "tp" "vr")) i)))
845 (numbered? (org-export-numbered-headline-p headline info))
846 (notoc? (org-export-excluded-from-toc-p headline info))
847 (command
848 (and
849 (not (org-export-low-level-p headline info))
850 (let ((class (plist-get info :texinfo-class)))
851 (pcase (assoc class (plist-get info :texinfo-classes))
852 (`(,_ ,_ . ,sections)
853 (pcase (nth (1- (org-export-get-relative-level headline info))
854 sections)
855 (`(,numbered ,unnumbered ,unnumbered-no-toc ,appendix)
856 (cond
857 ((org-not-nil
858 (org-export-get-node-property :APPENDIX headline t))
859 appendix)
860 (numbered? numbered)
861 (index unnumbered)
862 (notoc? unnumbered-no-toc)
863 (t unnumbered)))
864 (`nil nil)
865 (_ (user-error "Invalid Texinfo class specification: %S"
866 class))))
867 (_ (user-error "Unknown Texinfo class: %S" class))))))
868 (todo
869 (and (plist-get info :with-todo-keywords)
870 (let ((todo (org-element-property :todo-keyword headline)))
871 (and todo (org-export-data todo info)))))
872 (todo-type (and todo (org-element-property :todo-type headline)))
873 (tags (and (plist-get info :with-tags)
874 (org-export-get-tags headline info)))
875 (priority (and (plist-get info :with-priority)
876 (org-element-property :priority headline)))
877 (text (org-texinfo--sanitize-title
878 (org-element-property :title headline) info))
879 (full-text
880 (funcall (plist-get info :texinfo-format-headline-function)
881 todo todo-type priority text tags))
882 (contents
883 (concat "\n"
884 (if (org-string-nw-p contents) (concat "\n" contents) "")
885 (and index (format "\n@printindex %s\n" index)))))
886 (if (not command)
887 (concat (and (org-export-first-sibling-p headline info)
888 (format "@%s\n" (if numbered? 'enumerate 'itemize)))
889 "@item\n" full-text "\n"
890 contents
891 (if (org-export-last-sibling-p headline info)
892 (format "@end %s" (if numbered? 'enumerate 'itemize))
893 "\n"))
894 (concat
895 ;; Even if HEADLINE is using @subheading and al., leave an
896 ;; anchor so cross-references in the Org document still work.
897 (format (if notoc? "@anchor{%s}\n" "@node %s\n")
898 (org-texinfo--get-node headline info))
899 (format command full-text)
900 contents))))))
902 (defun org-texinfo-format-headline-default-function
903 (todo _todo-type priority text tags)
904 "Default format function for a headline.
905 See `org-texinfo-format-headline-function' for details."
906 (concat (when todo (format "@strong{%s} " todo))
907 (when priority (format "@emph{#%s} " priority))
908 text
909 (when tags (format " :%s:" (mapconcat 'identity tags ":")))))
911 ;;;; Inline Src Block
913 (defun org-texinfo-inline-src-block (inline-src-block _contents _info)
914 "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
915 CONTENTS holds the contents of the item. INFO is a plist holding
916 contextual information."
917 (format "@code{%s}"
918 (org-texinfo--sanitize-content
919 (org-element-property :value inline-src-block))))
921 ;;;; Inlinetask
923 (defun org-texinfo-inlinetask (inlinetask contents info)
924 "Transcode an INLINETASK element from Org to Texinfo.
925 CONTENTS holds the contents of the block. INFO is a plist
926 holding contextual information."
927 (let ((title (org-export-data (org-element-property :title inlinetask) info))
928 (todo (and (plist-get info :with-todo-keywords)
929 (let ((todo (org-element-property :todo-keyword inlinetask)))
930 (and todo (org-export-data todo info)))))
931 (todo-type (org-element-property :todo-type inlinetask))
932 (tags (and (plist-get info :with-tags)
933 (org-export-get-tags inlinetask info)))
934 (priority (and (plist-get info :with-priority)
935 (org-element-property :priority inlinetask))))
936 (funcall (plist-get info :texinfo-format-inlinetask-function)
937 todo todo-type priority title tags contents)))
939 (defun org-texinfo-format-inlinetask-default-function
940 (todo _todo-type priority title tags contents)
941 "Default format function for a inlinetasks.
942 See `org-texinfo-format-inlinetask-function' for details."
943 (let ((full-title
944 (concat (when todo (format "@strong{%s} " todo))
945 (when priority (format "#%c " priority))
946 title
947 (when tags (format ":%s:" (mapconcat #'identity tags ":"))))))
948 (format "@center %s\n\n%s\n" full-title contents)))
950 ;;;; Italic
952 (defun org-texinfo-italic (_italic contents info)
953 "Transcode ITALIC from Org to Texinfo.
954 CONTENTS is the text with italic markup. INFO is a plist holding
955 contextual information."
956 (org-texinfo--text-markup contents 'italic info))
958 ;;;; Item
960 (defun org-texinfo-item (item contents info)
961 "Transcode an ITEM element from Org to Texinfo.
962 CONTENTS holds the contents of the item. INFO is a plist holding
963 contextual information."
964 (let* ((tag (org-element-property :tag item))
965 (split (org-string-nw-p
966 (org-export-read-attribute :attr_texinfo
967 (org-element-property :parent item)
968 :sep)))
969 (items (and tag
970 (let ((tag (org-export-data tag info)))
971 (if split
972 (split-string tag (regexp-quote split) t "[ \t\n]+")
973 (list tag))))))
974 (format "%s\n%s"
975 (pcase items
976 (`nil "@item")
977 (`(,item) (concat "@item " item))
978 (`(,item . ,items)
979 (concat "@item " item "\n"
980 (mapconcat (lambda (i) (concat "@itemx " i))
981 items
982 "\n"))))
983 (or contents ""))))
985 ;;;; Keyword
987 (defun org-texinfo-keyword (keyword _contents info)
988 "Transcode a KEYWORD element from Org to Texinfo.
989 CONTENTS is nil. INFO is a plist holding contextual information."
990 (let ((value (org-element-property :value keyword)))
991 (pcase (org-element-property :key keyword)
992 ("TEXINFO" value)
993 ("CINDEX" (format "@cindex %s" value))
994 ("FINDEX" (format "@findex %s" value))
995 ("KINDEX" (format "@kindex %s" value))
996 ("PINDEX" (format "@pindex %s" value))
997 ("TINDEX" (format "@tindex %s" value))
998 ("VINDEX" (format "@vindex %s" value))
999 ("TOC"
1000 (cond ((string-match-p "\\<tables\\>" value)
1001 (concat "@listoffloats "
1002 (org-export-translate "Table" :utf-8 info)))
1003 ((string-match-p "\\<listings\\>" value)
1004 (concat "@listoffloats "
1005 (org-export-translate "Listing" :utf-8 info))))))))
1007 ;;;; Line Break
1009 (defun org-texinfo-line-break (_line-break _contents _info)
1010 "Transcode a LINE-BREAK object from Org to Texinfo.
1011 CONTENTS is nil. INFO is a plist holding contextual information."
1012 "@*\n")
1014 ;;;; Link
1016 (defun org-texinfo--@ref (datum description info)
1017 "Return @ref command for element or object DATUM.
1018 DESCRIPTION is the printed name of the section, as a string, or
1019 nil."
1020 (let ((node-name (org-texinfo--get-node datum info))
1021 ;; Sanitize DESCRIPTION for cross-reference use. In
1022 ;; particular, remove colons as they seem to cause pain (even
1023 ;; within @asis{...}) to the Texinfo reader.
1024 (title (and description
1025 (replace-regexp-in-string
1026 "[ \t]*:+" ""
1027 (replace-regexp-in-string "," "@comma{}" description)))))
1028 (if (or (not title) (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 headline desc info)))
1077 (_ (org-texinfo--@ref destination desc info)))))
1078 ((string= type "mailto")
1079 (format "@email{%s}"
1080 (concat (org-texinfo--sanitize-content path)
1081 (and desc (concat ", " desc)))))
1082 ;; External link with a description part.
1083 ((and path desc) (format "@uref{%s, %s}" path desc))
1084 ;; External link without a description part.
1085 (path (format "@uref{%s}" path))
1086 ;; No path, only description. Try to do something useful.
1088 (format (plist-get info :texinfo-link-with-unknown-path-format) desc)))))
1090 (defun org-texinfo--inline-image (link info)
1091 "Return Texinfo code for an inline image.
1092 LINK is the link pointing to the inline image. INFO is the
1093 current state of the export, as a plist."
1094 (let* ((parent (org-export-get-parent-element link))
1095 (label (and (org-element-property :name parent)
1096 (org-texinfo--get-node parent info)))
1097 (caption (org-export-get-caption parent))
1098 (shortcaption (org-export-get-caption parent t))
1099 (path (org-element-property :path link))
1100 (filename
1101 (file-name-sans-extension
1102 (if (file-name-absolute-p path) (expand-file-name path) path)))
1103 (extension (file-name-extension path))
1104 (attributes (org-export-read-attribute :attr_texinfo parent))
1105 (height (or (plist-get attributes :height) ""))
1106 (width (or (plist-get attributes :width) ""))
1107 (alt (or (plist-get attributes :alt) ""))
1108 (image (format "@image{%s,%s,%s,%s,%s}"
1109 filename width height alt extension)))
1110 (cond ((or caption shortcaption)
1111 (org-texinfo--wrap-float image
1112 info
1113 (org-export-translate "Figure" :utf-8 info)
1114 label
1115 caption
1116 shortcaption))
1117 (label (concat "@anchor{" label "}\n" image))
1118 (t image))))
1121 ;;;; Menu
1123 (defun org-texinfo-make-menu (scope info &optional master)
1124 "Create the menu for inclusion in the Texinfo document.
1126 SCOPE is a headline or a full parse tree. INFO is the
1127 communication channel, as a plist.
1129 When optional argument MASTER is non-nil, generate a master menu,
1130 including detailed node listing."
1131 (let ((menu (org-texinfo--build-menu scope info)))
1132 (when (org-string-nw-p menu)
1133 (org-element-normalize-string
1134 (format
1135 "@menu\n%s@end menu"
1136 (concat menu
1137 (when master
1138 (let ((detailmenu
1139 (org-texinfo--build-menu
1140 scope info
1141 (let ((toc-depth (plist-get info :with-toc)))
1142 (if (wholenump toc-depth) toc-depth
1143 org-texinfo-max-toc-depth)))))
1144 (when (org-string-nw-p detailmenu)
1145 (concat "\n@detailmenu\n"
1146 "--- The Detailed Node Listing ---\n\n"
1147 detailmenu
1148 "@end detailmenu\n"))))))))))
1150 (defun org-texinfo--build-menu (scope info &optional level)
1151 "Build menu for entries within SCOPE.
1152 SCOPE is a headline or a full parse tree. INFO is a plist
1153 containing contextual information. When optional argument LEVEL
1154 is an integer, build the menu recursively, down to this depth."
1155 (cond
1156 ((not level)
1157 (org-texinfo--format-entries (org-texinfo--menu-entries scope info) info))
1158 ((zerop level) "\n")
1160 (mapconcat
1161 (lambda (h)
1162 (let ((entries (org-texinfo--menu-entries h info)))
1163 (when entries
1164 (concat
1165 (format "%s\n\n%s\n"
1166 (org-export-data (org-export-get-alt-title h info) info)
1167 (org-texinfo--format-entries entries info))
1168 (org-texinfo--build-menu h info (1- level))))))
1169 (org-texinfo--menu-entries scope info)
1170 ""))))
1172 (defun org-texinfo--format-entries (entries info)
1173 "Format all direct menu entries in SCOPE, as a string.
1174 SCOPE is either a headline or a full Org document. INFO is
1175 a plist containing contextual information."
1176 (org-element-normalize-string
1177 (mapconcat
1178 (lambda (h)
1179 (let* ((title
1180 ;; Colons are used as a separator between title and node
1181 ;; name. Remove them.
1182 (replace-regexp-in-string
1183 "[ \t]+:+" ""
1184 (org-texinfo--sanitize-title
1185 (org-export-get-alt-title h info) info)))
1186 (node (org-texinfo--get-node h info))
1187 (entry (concat "* " title ":"
1188 (if (string= title node) ":"
1189 (concat " " node ". "))))
1190 (desc (org-element-property :DESCRIPTION h)))
1191 (if (not desc) entry
1192 (format (format "%%-%ds %%s" org-texinfo-node-description-column)
1193 entry desc))))
1194 entries "\n")))
1196 (defun org-texinfo--menu-entries (scope info)
1197 "List direct children in SCOPE needing a menu entry.
1198 SCOPE is a headline or a full parse tree. INFO is a plist
1199 holding contextual information."
1200 (let* ((cache (or (plist-get info :texinfo-entries-cache)
1201 (plist-get (plist-put info :texinfo-entries-cache
1202 (make-hash-table :test #'eq))
1203 :texinfo-entries-cache)))
1204 (cached-entries (gethash scope cache 'no-cache)))
1205 (if (not (eq cached-entries 'no-cache)) cached-entries
1206 (puthash scope
1207 (cl-remove-if
1208 (lambda (h)
1209 (org-not-nil (org-export-get-node-property :COPYING h t)))
1210 (org-export-collect-headlines info 1 scope))
1211 cache))))
1213 ;;;; Node Property
1215 (defun org-texinfo-node-property (node-property _contents _info)
1216 "Transcode a NODE-PROPERTY element from Org to Texinfo.
1217 CONTENTS is nil. INFO is a plist holding contextual
1218 information."
1219 (format "%s:%s"
1220 (org-element-property :key node-property)
1221 (let ((value (org-element-property :value node-property)))
1222 (if value (concat " " value) ""))))
1224 ;;;; Paragraph
1226 (defun org-texinfo-paragraph (_paragraph contents _info)
1227 "Transcode a PARAGRAPH element from Org to Texinfo.
1228 CONTENTS is the contents of the paragraph, as a string. INFO is
1229 the plist used as a communication channel."
1230 contents)
1232 ;;;; Plain List
1234 (defun org-texinfo-plain-list (plain-list contents info)
1235 "Transcode a PLAIN-LIST element from Org to Texinfo.
1236 CONTENTS is the contents of the list. INFO is a plist holding
1237 contextual information."
1238 (let* ((attr (org-export-read-attribute :attr_texinfo plain-list))
1239 (indic (let ((i (or (plist-get attr :indic)
1240 (plist-get info :texinfo-table-default-markup))))
1241 ;; Allow indicating commands with missing @ sign.
1242 (if (string-prefix-p "@" i) i (concat "@" i))))
1243 (table-type (plist-get attr :table-type))
1244 (type (org-element-property :type plain-list))
1245 (list-type (cond
1246 ((eq type 'ordered) "enumerate")
1247 ((eq type 'unordered) "itemize")
1248 ((member table-type '("ftable" "vtable")) table-type)
1249 (t "table"))))
1250 (format "@%s\n%s@end %s"
1251 (if (eq type 'descriptive) (concat list-type " " indic) list-type)
1252 contents
1253 list-type)))
1255 ;;;; Plain Text
1257 (defun org-texinfo-plain-text (text info)
1258 "Transcode a TEXT string from Org to Texinfo.
1259 TEXT is the string to transcode. INFO is a plist holding
1260 contextual information."
1261 ;; First protect @, { and }.
1262 (let ((output (org-texinfo--sanitize-content text)))
1263 ;; Activate smart quotes. Be sure to provide original TEXT string
1264 ;; since OUTPUT may have been modified.
1265 (when (plist-get info :with-smart-quotes)
1266 (setq output
1267 (org-export-activate-smart-quotes output :texinfo info text)))
1268 ;; LaTeX into @LaTeX{} and TeX into @TeX{}
1269 (let ((case-fold-search nil))
1270 (setq output (replace-regexp-in-string "\\(?:La\\)?TeX" "@\\&{}" output)))
1271 ;; Convert special strings.
1272 (when (plist-get info :with-special-strings)
1273 (setq output
1274 (replace-regexp-in-string
1275 "\\.\\.\\." "@dots{}"
1276 (replace-regexp-in-string "\\\\-" "@-" output))))
1277 ;; Handle break preservation if required.
1278 (when (plist-get info :preserve-breaks)
1279 (setq output (replace-regexp-in-string
1280 "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" output)))
1281 ;; Return value.
1282 output))
1284 ;;;; Planning
1286 (defun org-texinfo-planning (planning _contents info)
1287 "Transcode a PLANNING element from Org to Texinfo.
1288 CONTENTS is nil. INFO is a plist holding contextual
1289 information."
1290 (concat
1291 "@noindent"
1292 (mapconcat
1293 'identity
1294 (delq nil
1295 (list
1296 (let ((closed (org-element-property :closed planning)))
1297 (when closed
1298 (concat
1299 (format "@strong{%s} " org-closed-string)
1300 (format (plist-get info :texinfo-inactive-timestamp-format)
1301 (org-timestamp-translate closed)))))
1302 (let ((deadline (org-element-property :deadline planning)))
1303 (when deadline
1304 (concat
1305 (format "@strong{%s} " org-deadline-string)
1306 (format (plist-get info :texinfo-active-timestamp-format)
1307 (org-timestamp-translate deadline)))))
1308 (let ((scheduled (org-element-property :scheduled planning)))
1309 (when scheduled
1310 (concat
1311 (format "@strong{%s} " org-scheduled-string)
1312 (format (plist-get info :texinfo-active-timestamp-format)
1313 (org-timestamp-translate scheduled)))))))
1314 " ")
1315 "@*"))
1317 ;;;; Property Drawer
1319 (defun org-texinfo-property-drawer (_property-drawer contents _info)
1320 "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
1321 CONTENTS holds the contents of the drawer. INFO is a plist
1322 holding contextual information."
1323 (and (org-string-nw-p contents)
1324 (format "@verbatim\n%s@end verbatim" contents)))
1326 ;;;; Quote Block
1328 (defun org-texinfo-quote-block (quote-block contents _info)
1329 "Transcode a QUOTE-BLOCK element from Org to Texinfo.
1330 CONTENTS holds the contents of the block. INFO is a plist
1331 holding contextual information."
1332 (let* ((title (org-element-property :name quote-block))
1333 (start-quote (concat "@quotation"
1334 (if title
1335 (format " %s" title)))))
1336 (format "%s\n%s@end quotation" start-quote contents)))
1338 ;;;; Radio Target
1340 (defun org-texinfo-radio-target (radio-target text info)
1341 "Transcode a RADIO-TARGET object from Org to Texinfo.
1342 TEXT is the text of the target. INFO is a plist holding
1343 contextual information."
1344 (format "@anchor{%s}%s"
1345 (org-texinfo--get-node radio-target info)
1346 text))
1348 ;;;; Section
1350 (defun org-texinfo-section (section contents info)
1351 "Transcode a SECTION element from Org to Texinfo.
1352 CONTENTS holds the contents of the section. INFO is a plist
1353 holding contextual information."
1354 (let ((parent (org-export-get-parent-headline section)))
1355 (when parent ;first section is handled in `org-texinfo-template'
1356 (org-trim
1357 (concat contents
1358 "\n"
1359 (and (not (org-export-excluded-from-toc-p parent info))
1360 (org-texinfo-make-menu parent info)))))))
1362 ;;;; Special Block
1364 (defun org-texinfo-special-block (special-block contents _info)
1365 "Transcode a SPECIAL-BLOCK element from Org to Texinfo.
1366 CONTENTS holds the contents of the block. INFO is a plist used
1367 as a communication channel."
1368 (let ((opt (org-export-read-attribute :attr_texinfo special-block :options))
1369 (type (org-element-property :type special-block)))
1370 (format "@%s%s\n%s@end %s"
1371 type
1372 (if opt (concat " " opt) "")
1373 (or contents "")
1374 type)))
1376 ;;;; Src Block
1378 (defun org-texinfo-src-block (src-block _contents info)
1379 "Transcode a SRC-BLOCK element from Org to Texinfo.
1380 CONTENTS holds the contents of the item. INFO is a plist holding
1381 contextual information."
1382 (let* ((lisp (string-match-p "lisp"
1383 (org-element-property :language src-block)))
1384 (code (org-texinfo--sanitize-content
1385 (org-export-format-code-default src-block info)))
1386 (value (format
1387 (if lisp "@lisp\n%s@end lisp" "@example\n%s@end example")
1388 code))
1389 (caption (org-export-get-caption src-block))
1390 (shortcaption (org-export-get-caption src-block t)))
1391 (if (not (or caption shortcaption)) value
1392 (org-texinfo--wrap-float value
1393 info
1394 (org-export-translate "Listing" :utf-8 info)
1395 (org-texinfo--get-node src-block info)
1396 caption
1397 shortcaption))))
1399 ;;;; Statistics Cookie
1401 (defun org-texinfo-statistics-cookie (statistics-cookie _contents _info)
1402 "Transcode a STATISTICS-COOKIE object from Org to Texinfo.
1403 CONTENTS is nil. INFO is a plist holding contextual information."
1404 (org-element-property :value statistics-cookie))
1407 ;;;; Strike-through
1409 (defun org-texinfo-strike-through (_strike-through contents info)
1410 "Transcode STRIKE-THROUGH from Org to Texinfo.
1411 CONTENTS is the text with strike-through markup. INFO is a plist
1412 holding contextual information."
1413 (org-texinfo--text-markup contents 'strike-through info))
1415 ;;;; Subscript
1417 (defun org-texinfo-subscript (_subscript contents _info)
1418 "Transcode a SUBSCRIPT object from Org to Texinfo.
1419 CONTENTS is the contents of the object. INFO is a plist holding
1420 contextual information."
1421 (format "@math{_%s}" contents))
1423 ;;;; Superscript
1425 (defun org-texinfo-superscript (_superscript contents _info)
1426 "Transcode a SUPERSCRIPT object from Org to Texinfo.
1427 CONTENTS is the contents of the object. INFO is a plist holding
1428 contextual information."
1429 (format "@math{^%s}" contents))
1431 ;;;; Table
1433 (defun org-texinfo-table (table contents info)
1434 "Transcode a TABLE element from Org to Texinfo.
1435 CONTENTS is the contents of the table. INFO is a plist holding
1436 contextual information."
1437 (if (eq (org-element-property :type table) 'table.el)
1438 (format "@verbatim\n%s@end verbatim"
1439 (org-element-normalize-string
1440 (org-element-property :value table)))
1441 (let* ((col-width (org-export-read-attribute :attr_texinfo table :columns))
1442 (columns
1443 (if col-width (format "@columnfractions %s" col-width)
1444 (org-texinfo-table-column-widths table info)))
1445 (caption (org-export-get-caption table))
1446 (shortcaption (org-export-get-caption table t))
1447 (table-str (format "@multitable %s\n%s@end multitable"
1448 columns
1449 contents)))
1450 (if (not (or caption shortcaption)) table-str
1451 (org-texinfo--wrap-float table-str
1452 info
1453 (org-export-translate "Table" :utf-8 info)
1454 (org-texinfo--get-node table info)
1455 caption
1456 shortcaption)))))
1458 (defun org-texinfo-table-column-widths (table info)
1459 "Determine the largest table cell in each column to process alignment.
1460 TABLE is the table element to transcode. INFO is a plist used as
1461 a communication channel."
1462 (let ((widths (make-vector (cdr (org-export-table-dimensions table info)) 0)))
1463 (org-element-map table 'table-row
1464 (lambda (row)
1465 (let ((idx 0))
1466 (org-element-map row 'table-cell
1467 (lambda (cell)
1468 ;; Length of the cell in the original buffer is only an
1469 ;; approximation of the length of the cell in the
1470 ;; output. It can sometimes fail (e.g. it considers
1471 ;; "/a/" being larger than "ab").
1472 (let ((w (- (org-element-property :contents-end cell)
1473 (org-element-property :contents-begin cell))))
1474 (aset widths idx (max w (aref widths idx))))
1475 (cl-incf idx))
1476 info)))
1477 info)
1478 (format "{%s}" (mapconcat (lambda (w) (make-string w ?a)) widths "} {"))))
1480 ;;;; Table Cell
1482 (defun org-texinfo-table-cell (table-cell contents info)
1483 "Transcode a TABLE-CELL element from Org to Texinfo.
1484 CONTENTS is the cell contents. INFO is a plist used as
1485 a communication channel."
1486 (concat
1487 (let ((scientific-notation
1488 (plist-get info :texinfo-table-scientific-notation)))
1489 (if (and contents
1490 scientific-notation
1491 (string-match orgtbl-exp-regexp contents))
1492 ;; Use appropriate format string for scientific notation.
1493 (format scientific-notation
1494 (match-string 1 contents)
1495 (match-string 2 contents))
1496 contents))
1497 (when (org-export-get-next-element table-cell info) "\n@tab ")))
1499 ;;;; Table Row
1501 (defun org-texinfo-table-row (table-row contents info)
1502 "Transcode a TABLE-ROW element from Org to Texinfo.
1503 CONTENTS is the contents of the row. INFO is a plist used as
1504 a communication channel."
1505 ;; Rules are ignored since table separators are deduced from
1506 ;; borders of the current row.
1507 (when (eq (org-element-property :type table-row) 'standard)
1508 (let ((rowgroup-tag
1509 (if (and (= 1 (org-export-table-row-group table-row info))
1510 (org-export-table-has-header-p
1511 (org-export-get-parent-table table-row) info))
1512 "@headitem "
1513 "@item ")))
1514 (concat rowgroup-tag contents "\n"))))
1516 ;;;; Target
1518 (defun org-texinfo-target (target _contents info)
1519 "Transcode a TARGET object from Org to Texinfo.
1520 CONTENTS is nil. INFO is a plist holding contextual
1521 information."
1522 (format "@anchor{%s}" (org-texinfo--get-node target info)))
1524 ;;;; Timestamp
1526 (defun org-texinfo-timestamp (timestamp _contents info)
1527 "Transcode a TIMESTAMP object from Org to Texinfo.
1528 CONTENTS is nil. INFO is a plist holding contextual
1529 information."
1530 (let ((value (org-texinfo-plain-text
1531 (org-timestamp-translate timestamp) info)))
1532 (pcase (org-element-property :type timestamp)
1533 ((or `active `active-range)
1534 (format (plist-get info :texinfo-active-timestamp-format) value))
1535 ((or `inactive `inactive-range)
1536 (format (plist-get info :texinfo-inactive-timestamp-format) value))
1537 (_ (format (plist-get info :texinfo-diary-timestamp-format) value)))))
1539 ;;;; Underline
1541 (defun org-texinfo-underline (_underline contents info)
1542 "Transcode UNDERLINE from Org to Texinfo.
1543 CONTENTS is the text with underline markup. INFO is a plist
1544 holding contextual information."
1545 (org-texinfo--text-markup contents 'underline info))
1547 ;;;; Verbatim
1549 (defun org-texinfo-verbatim (verbatim _contents info)
1550 "Transcode a VERBATIM object from Org to Texinfo.
1551 CONTENTS is nil. INFO is a plist used as a communication
1552 channel."
1553 (org-texinfo--text-markup
1554 (org-element-property :value verbatim) 'verbatim info))
1556 ;;;; Verse Block
1558 (defun org-texinfo-verse-block (_verse-block contents _info)
1559 "Transcode a VERSE-BLOCK element from Org to Texinfo.
1560 CONTENTS is verse block contents. INFO is a plist holding
1561 contextual information."
1562 (format "@display\n%s@end display" contents))
1565 ;;; Interactive functions
1567 ;;;###autoload
1568 (defun org-texinfo-export-to-texinfo
1569 (&optional async subtreep visible-only body-only ext-plist)
1570 "Export current buffer to a Texinfo file.
1572 If narrowing is active in the current buffer, only export its
1573 narrowed part.
1575 If a region is active, export that region.
1577 A non-nil optional argument ASYNC means the process should happen
1578 asynchronously. The resulting file should be accessible through
1579 the `org-export-stack' interface.
1581 When optional argument SUBTREEP is non-nil, export the sub-tree
1582 at point, extracting information from the headline properties
1583 first.
1585 When optional argument VISIBLE-ONLY is non-nil, don't export
1586 contents of hidden elements.
1588 When optional argument BODY-ONLY is non-nil, only write code
1589 between \"\\begin{document}\" and \"\\end{document}\".
1591 EXT-PLIST, when provided, is a property list with external
1592 parameters overriding Org default settings, but still inferior to
1593 file-local settings.
1595 Return output file's name."
1596 (interactive)
1597 (let ((outfile (org-export-output-file-name ".texi" subtreep))
1598 (org-export-coding-system org-texinfo-coding-system))
1599 (org-export-to-file 'texinfo outfile
1600 async subtreep visible-only body-only ext-plist)))
1602 ;;;###autoload
1603 (defun org-texinfo-export-to-info
1604 (&optional async subtreep visible-only body-only ext-plist)
1605 "Export current buffer to Texinfo then process through to INFO.
1607 If narrowing is active in the current buffer, only export its
1608 narrowed part.
1610 If a region is active, export that region.
1612 A non-nil optional argument ASYNC means the process should happen
1613 asynchronously. The resulting file should be accessible through
1614 the `org-export-stack' interface.
1616 When optional argument SUBTREEP is non-nil, export the sub-tree
1617 at point, extracting information from the headline properties
1618 first.
1620 When optional argument VISIBLE-ONLY is non-nil, don't export
1621 contents of hidden elements.
1623 When optional argument BODY-ONLY is non-nil, only write code
1624 between \"\\begin{document}\" and \"\\end{document}\".
1626 EXT-PLIST, when provided, is a property list with external
1627 parameters overriding Org default settings, but still inferior to
1628 file-local settings.
1630 When optional argument PUB-DIR is set, use it as the publishing
1631 directory.
1633 Return INFO file's name."
1634 (interactive)
1635 (let ((outfile (org-export-output-file-name ".texi" subtreep))
1636 (org-export-coding-system org-texinfo-coding-system))
1637 (org-export-to-file 'texinfo outfile
1638 async subtreep visible-only body-only ext-plist
1639 (lambda (file) (org-texinfo-compile file)))))
1641 ;;;###autoload
1642 (defun org-texinfo-publish-to-texinfo (plist filename pub-dir)
1643 "Publish an org file to Texinfo.
1645 FILENAME is the filename of the Org file to be published. PLIST
1646 is the property list for the given project. PUB-DIR is the
1647 publishing directory.
1649 Return output file name."
1650 (org-publish-org-to 'texinfo filename ".texi" plist pub-dir))
1652 ;;;###autoload
1653 (defun org-texinfo-convert-region-to-texinfo ()
1654 "Assume the current region has Org syntax, and convert it to Texinfo.
1655 This can be used in any buffer. For example, you can write an
1656 itemized list in Org syntax in an Texinfo buffer and use this
1657 command to convert it."
1658 (interactive)
1659 (org-export-replace-region-by 'texinfo))
1661 (defun org-texinfo-compile (file)
1662 "Compile a texinfo file.
1664 FILE is the name of the file being compiled. Processing is done
1665 through the command specified in `org-texinfo-info-process',
1666 which see. Output is redirected to \"*Org INFO Texinfo Output*\"
1667 buffer.
1669 Return INFO file name or an error if it couldn't be produced."
1670 (message "Processing Texinfo file %s..." file)
1671 (let* ((log-name "*Org INFO Texinfo Output*")
1672 (log (get-buffer-create log-name))
1673 (output
1674 (org-compile-file file org-texinfo-info-process "info"
1675 (format "See %S for details" log-name)
1676 log)))
1677 (when org-texinfo-remove-logfiles
1678 (let ((base (file-name-sans-extension output)))
1679 (dolist (ext org-texinfo-logfiles-extensions)
1680 (let ((file (concat base "." ext)))
1681 (when (file-exists-p file) (delete-file file))))))
1682 (message "Process completed.")
1683 output))
1686 (provide 'ox-texinfo)
1688 ;; Local variables:
1689 ;; generated-autoload-file: "org-loaddefs.el"
1690 ;; End:
1692 ;;; ox-texinfo.el ends here