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