ox-texinfo: Improve caption handling
[org-mode.git] / lisp / ox-texinfo.el
blob47d593be6442e63a489bcfc060cbd673f39a8c90
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 ;;;; Menu sanitizing
481 (defun org-texinfo--sanitize-node (title)
482 "Bend string TITLE to node line requirements.
483 Trim string and collapse multiple whitespace characters as they
484 are not significant. Also remove the following characters: @
485 { } ( ) : . ,"
486 (replace-regexp-in-string
487 "[:,.]" ""
488 (replace-regexp-in-string
489 "\\`(\\(.*)\\)" "[\\1"
490 (org-trim (replace-regexp-in-string "[ \t]\\{2,\\}" " " title)))))
492 ;;;; Content sanitizing
494 (defun org-texinfo--sanitize-content (text)
495 "Escape special characters in string TEXT.
496 Special characters are: @ { }"
497 (replace-regexp-in-string "[@{}]" "@\\&" text))
499 ;;; Template
501 (defun org-texinfo-template (contents info)
502 "Return complete document string after Texinfo conversion.
503 CONTENTS is the transcoded contents string. INFO is a plist
504 holding export options."
505 (let ((title (org-export-data (plist-get info :title) info))
506 ;; Copying data is the contents of the first headline in
507 ;; parse tree with a non-nil copying property.
508 (copying (org-element-map (plist-get info :parse-tree) 'headline
509 (lambda (hl)
510 (and (org-not-nil (org-element-property :COPYING hl))
511 (org-element-contents hl)))
512 info t)))
513 (concat
514 "\\input texinfo @c -*- texinfo -*-\n"
515 "@c %**start of header\n"
516 (let ((file (or (plist-get info :texinfo-filename)
517 (let ((f (plist-get info :output-file)))
518 (and f (concat (file-name-sans-extension f) ".info"))))))
519 (and file (format "@setfilename %s\n" file)))
520 (format "@settitle %s\n" title)
521 ;; Insert class-defined header.
522 (org-element-normalize-string
523 (let ((header (nth 1 (assoc (plist-get info :texinfo-class)
524 org-texinfo-classes)))
525 (coding
526 (catch 'coding-system
527 (let ((case-fold-search t)
528 (name (symbol-name (or org-texinfo-coding-system
529 buffer-file-coding-system))))
530 (dolist (system org-texinfo-supported-coding-systems "UTF-8")
531 (when (org-string-match-p (regexp-quote system) name)
532 (throw 'coding-system system))))))
533 (language (plist-get info :language))
534 (case-fold-search nil))
535 ;; Auto coding system.
536 (replace-regexp-in-string
537 "^@documentencoding \\(AUTO\\)$"
538 coding
539 (replace-regexp-in-string
540 "^@documentlanguage \\(AUTO\\)$" language header t nil 1) t nil 1)))
541 ;; Additional header options set by #+TEXINFO_HEADER.
542 (let ((texinfo-header (plist-get info :texinfo-header)))
543 (and texinfo-header (org-element-normalize-string texinfo-header)))
544 "@c %**end of header\n\n"
545 ;; Additional options set by #+TEXINFO_POST_HEADER.
546 (let ((texinfo-post-header (plist-get info :texinfo-post-header)))
547 (and texinfo-post-header
548 (org-element-normalize-string texinfo-post-header)))
549 ;; Copying.
550 (and copying
551 (format "@copying\n%s@end copying\n\n"
552 (org-element-normalize-string
553 (org-export-data copying info))))
554 ;; Info directory information. Only supply if both title and
555 ;; category are provided.
556 (let ((dircat (plist-get info :texinfo-dircat))
557 (dirtitle
558 (let ((title (plist-get info :texinfo-dirtitle)))
559 (and title
560 (string-match "^\\(?:\\* \\)?\\(.*?\\)\\(\\.\\)?$" title)
561 (format "* %s." (match-string 1 title))))))
562 (when (and dircat dirtitle)
563 (concat "@dircategory " dircat "\n"
564 "@direntry\n"
565 (let ((dirdesc
566 (let ((desc (plist-get info :texinfo-dirdesc)))
567 (cond ((not desc) nil)
568 ((org-string-match-p "\\.$" desc) desc)
569 (t (concat desc "."))))))
570 (if dirdesc (format "%-23s %s" dirtitle dirdesc) dirtitle))
571 "\n"
572 "@end direntry\n\n")))
573 ;; Title
574 "@finalout\n"
575 "@titlepage\n"
576 (when (plist-get info :with-title)
577 (concat
578 (format "@title %s\n" (or (plist-get info :texinfo-printed-title) title ""))
579 (let ((subtitle (plist-get info :subtitle)))
580 (when subtitle
581 (format "@subtitle %s\n"
582 (org-export-data subtitle info))))))
583 (when (plist-get info :with-author)
584 (concat
585 ;; Primary author.
586 (let ((author (org-string-nw-p
587 (org-export-data (plist-get info :author) info)))
588 (email (and (plist-get info :with-email)
589 (org-string-nw-p
590 (org-export-data (plist-get info :email) info)))))
591 (cond ((and author email)
592 (format "@author %s (@email{%s})\n" author email))
593 (author (format "@author %s\n" author))
594 (email (format "@author @email{%s}\n" email))))
595 ;; Other authors.
596 (let ((subauthor (plist-get info :subauthor)))
597 (and subauthor
598 (org-element-normalize-string
599 (replace-regexp-in-string "^" "@author " subauthor))))))
600 (and copying "@page\n@vskip 0pt plus 1filll\n@insertcopying\n")
601 "@end titlepage\n\n"
602 ;; Table of contents.
603 (and (plist-get info :with-toc) "@contents\n\n")
604 ;; Configure Top Node when not for Tex
605 "@ifnottex\n"
606 "@node Top\n"
607 (format "@top %s\n" title)
608 (and copying "@insertcopying\n")
609 "@end ifnottex\n\n"
610 ;; Menu.
611 (org-texinfo-make-menu (plist-get info :parse-tree) info 'master)
612 "\n"
613 ;; Document's body.
614 contents "\n"
615 ;; Creator.
616 (and (plist-get info :with-creator)
617 (concat (plist-get info :creator) "\n"))
618 ;; Document end.
619 "@bye")))
623 ;;; Transcode Functions
625 ;;;; Bold
627 (defun org-texinfo-bold (bold contents info)
628 "Transcode BOLD from Org to Texinfo.
629 CONTENTS is the text with bold markup. INFO is a plist holding
630 contextual information."
631 (org-texinfo--text-markup contents 'bold info))
633 ;;;; Center Block
635 (defun org-texinfo-center-block (center-block contents info)
636 "Transcode a CENTER-BLOCK element from Org to Texinfo.
637 CONTENTS holds the contents of the block. INFO is a plist used
638 as a communication channel."
639 contents)
641 ;;;; Clock
643 (defun org-texinfo-clock (clock contents info)
644 "Transcode a CLOCK element from Org to Texinfo.
645 CONTENTS is nil. INFO is a plist holding contextual
646 information."
647 (concat
648 "@noindent"
649 (format "@strong{%s} " org-clock-string)
650 (format (plist-get info :texinfo-inactive-timestamp-format)
651 (concat (org-timestamp-translate (org-element-property :value clock))
652 (let ((time (org-element-property :duration clock)))
653 (and time (format " (%s)" time)))))
654 "@*"))
656 ;;;; Code
658 (defun org-texinfo-code (code contents info)
659 "Transcode a CODE object from Org to Texinfo.
660 CONTENTS is nil. INFO is a plist used as a communication
661 channel."
662 (org-texinfo--text-markup (org-element-property :value code) 'code info))
664 ;;;; Drawer
666 (defun org-texinfo-drawer (drawer contents info)
667 "Transcode a DRAWER element from Org to Texinfo.
668 CONTENTS holds the contents of the block. INFO is a plist
669 holding contextual information."
670 (let* ((name (org-element-property :drawer-name drawer))
671 (output (funcall (plist-get info :texinfo-format-drawer-function)
672 name contents)))
673 output))
675 ;;;; Dynamic Block
677 (defun org-texinfo-dynamic-block (dynamic-block contents info)
678 "Transcode a DYNAMIC-BLOCK element from Org to Texinfo.
679 CONTENTS holds the contents of the block. INFO is a plist
680 holding contextual information."
681 contents)
683 ;;;; Entity
685 (defun org-texinfo-entity (entity contents info)
686 "Transcode an ENTITY object from Org to Texinfo.
687 CONTENTS are the definition itself. INFO is a plist holding
688 contextual information."
689 (let ((ent (org-element-property :latex entity)))
690 (if (org-element-property :latex-math-p entity) (format "@math{%s}" ent) ent)))
692 ;;;; Example Block
694 (defun org-texinfo-example-block (example-block contents info)
695 "Transcode an EXAMPLE-BLOCK element from Org to Texinfo.
696 CONTENTS is nil. INFO is a plist holding contextual
697 information."
698 (format "@verbatim\n%s@end verbatim"
699 (org-export-format-code-default example-block info)))
701 ;;; Export Block
703 (defun org-texinfo-export-block (export-block contents info)
704 "Transcode a EXPORT-BLOCK element from Org to Texinfo.
705 CONTENTS is nil. INFO is a plist holding contextual information."
706 (when (string= (org-element-property :type export-block) "TEXINFO")
707 (org-remove-indentation (org-element-property :value export-block))))
709 ;;; Export Snippet
711 (defun org-texinfo-export-snippet (export-snippet contents info)
712 "Transcode a EXPORT-SNIPPET object from Org to Texinfo.
713 CONTENTS is nil. INFO is a plist holding contextual information."
714 (when (eq (org-export-snippet-backend export-snippet) 'texinfo)
715 (org-element-property :value export-snippet)))
717 ;;;; Fixed Width
719 (defun org-texinfo-fixed-width (fixed-width contents info)
720 "Transcode a FIXED-WIDTH element from Org to Texinfo.
721 CONTENTS is nil. INFO is a plist holding contextual information."
722 (format "@example\n%s\n@end example"
723 (org-remove-indentation
724 (org-texinfo--sanitize-content
725 (org-element-property :value fixed-width)))))
727 ;;;; Footnote Reference
729 (defun org-texinfo-footnote-reference (footnote contents info)
730 "Create a footnote reference for FOOTNOTE.
732 FOOTNOTE is the footnote to define. CONTENTS is nil. INFO is a
733 plist holding contextual information."
734 (let ((def (org-export-get-footnote-definition footnote info)))
735 (format "@footnote{%s}"
736 (org-trim (org-export-data def info)))))
738 ;;;; Headline
740 (defun org-texinfo-headline (headline contents info)
741 "Transcode a HEADLINE element from Org to Texinfo.
742 CONTENTS holds the contents of the headline. INFO is a plist
743 holding contextual information."
744 (let* ((class (plist-get info :texinfo-class))
745 (level (org-export-get-relative-level headline info))
746 (numberedp (org-export-numbered-headline-p headline info))
747 (class-sectioning (assoc class (plist-get info :texinfo-classes)))
748 ;; Find the index type, if any.
749 (index (org-element-property :INDEX headline))
750 ;; Create node info, to insert it before section formatting.
751 ;; Use custom menu title if present.
752 (node (format "@node %s\n" (org-texinfo--get-node headline info)))
753 ;; Section formatting will set two placeholders: one for the
754 ;; title and the other for the contents.
755 (section-fmt
756 (if (org-not-nil (org-element-property :APPENDIX headline))
757 "@appendix %s\n%s"
758 (let ((sec (if (and (symbolp (nth 2 class-sectioning))
759 (fboundp (nth 2 class-sectioning)))
760 (funcall (nth 2 class-sectioning) level numberedp)
761 (nth (1+ level) class-sectioning))))
762 (cond
763 ;; No section available for that LEVEL.
764 ((not sec) nil)
765 ;; Section format directly returned by a function.
766 ((stringp sec) sec)
767 ;; (numbered-section . unnumbered-section)
768 ((not (consp (cdr sec)))
769 (concat (if (or index (not numberedp)) (cdr sec) (car sec))
770 "\n%s"))))))
771 (todo
772 (and (plist-get info :with-todo-keywords)
773 (let ((todo (org-element-property :todo-keyword headline)))
774 (and todo (org-export-data todo info)))))
775 (todo-type (and todo (org-element-property :todo-type headline)))
776 (tags (and (plist-get info :with-tags)
777 (org-export-get-tags headline info)))
778 (priority (and (plist-get info :with-priority)
779 (org-element-property :priority headline)))
780 (text (org-export-data (org-element-property :title headline) info))
781 (full-text (funcall (plist-get info :texinfo-format-headline-function)
782 todo todo-type priority text tags))
783 (contents (if (org-string-nw-p contents) (concat "\n" contents) "")))
784 (cond
785 ;; Case 1: This is a footnote section: ignore it.
786 ((org-element-property :footnote-section-p headline) nil)
787 ;; Case 2: This is the `copying' section: ignore it
788 ;; This is used elsewhere.
789 ((org-not-nil (org-element-property :COPYING headline)) nil)
790 ;; Case 3: An index. If it matches one of the known indexes,
791 ;; print it as such following the contents, otherwise
792 ;; print the contents and leave the index up to the user.
793 (index
794 (concat node
795 (format
796 section-fmt
797 full-text
798 (concat contents
799 (and (member index '("cp" "fn" "ky" "pg" "tp" "vr"))
800 (concat "\n@printindex " index))))))
801 ;; Case 4: This is a deep sub-tree: export it as a list item.
802 ;; Also export as items headlines for which no section
803 ;; format has been found.
804 ((or (not section-fmt) (org-export-low-level-p headline info))
805 ;; Build the real contents of the sub-tree.
806 (concat (and (org-export-first-sibling-p headline info)
807 (format "@%s\n" (if numberedp 'enumerate 'itemize)))
808 "@item\n" full-text "\n"
809 contents
810 (if (org-export-last-sibling-p headline info)
811 (format "@end %s" (if numberedp 'enumerate 'itemize))
812 "\n")))
813 ;; Case 5: Standard headline. Export it as a section.
814 (t (concat node (format section-fmt full-text contents))))))
816 (defun org-texinfo-format-headline-default-function
817 (todo todo-type priority text tags)
818 "Default format function for a headline.
819 See `org-texinfo-format-headline-function' for details."
820 (concat (when todo (format "@strong{%s} " todo))
821 (when priority (format "@emph{#%s} " priority))
822 text
823 (when tags (format " :%s:" (mapconcat 'identity tags ":")))))
825 ;;;; Inline Src Block
827 (defun org-texinfo-inline-src-block (inline-src-block contents info)
828 "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
829 CONTENTS holds the contents of the item. INFO is a plist holding
830 contextual information."
831 (let* ((code (org-element-property :value inline-src-block))
832 (separator (org-texinfo--find-verb-separator code)))
833 (concat "@verb{" separator code separator "}")))
835 ;;;; Inlinetask
837 (defun org-texinfo-inlinetask (inlinetask contents info)
838 "Transcode an INLINETASK element from Org to Texinfo.
839 CONTENTS holds the contents of the block. INFO is a plist
840 holding contextual information."
841 (let ((title (org-export-data (org-element-property :title inlinetask) info))
842 (todo (and (plist-get info :with-todo-keywords)
843 (let ((todo (org-element-property :todo-keyword inlinetask)))
844 (and todo (org-export-data todo info)))))
845 (todo-type (org-element-property :todo-type inlinetask))
846 (tags (and (plist-get info :with-tags)
847 (org-export-get-tags inlinetask info)))
848 (priority (and (plist-get info :with-priority)
849 (org-element-property :priority inlinetask))))
850 (funcall (plist-get info :texinfo-format-inlinetask-function)
851 todo todo-type priority title tags contents)))
853 (defun org-texinfo-format-inlinetask-default-function
854 (todo todo-type priority title tags contents)
855 "Default format function for a inlinetasks.
856 See `org-texinfo-format-inlinetask-function' for details."
857 (let ((full-title
858 (concat (when todo (format "@strong{%s} " todo))
859 (when priority (format "#%c " priority))
860 title
861 (when tags (format ":%s:" (mapconcat #'identity tags ":"))))))
862 (format "@center %s\n\n%s\n" full-title contents)))
864 ;;;; Italic
866 (defun org-texinfo-italic (italic contents info)
867 "Transcode ITALIC from Org to Texinfo.
868 CONTENTS is the text with italic markup. INFO is a plist holding
869 contextual information."
870 (org-texinfo--text-markup contents 'italic info))
872 ;;;; Item
874 (defun org-texinfo-item (item contents info)
875 "Transcode an ITEM element from Org to Texinfo.
876 CONTENTS holds the contents of the item. INFO is a plist holding
877 contextual information."
878 (format "@item%s\n%s"
879 (let ((tag (org-element-property :tag item)))
880 (if tag (concat " " (org-export-data tag info)) ""))
881 (or contents "")))
883 ;;;; Keyword
885 (defun org-texinfo-keyword (keyword contents info)
886 "Transcode a KEYWORD element from Org to Texinfo.
887 CONTENTS is nil. INFO is a plist holding contextual information."
888 (let ((key (org-element-property :key keyword))
889 (value (org-element-property :value keyword)))
890 (cond
891 ((string= key "TEXINFO") value)
892 ((string= key "CINDEX") (format "@cindex %s" value))
893 ((string= key "FINDEX") (format "@findex %s" value))
894 ((string= key "KINDEX") (format "@kindex %s" value))
895 ((string= key "PINDEX") (format "@pindex %s" value))
896 ((string= key "TINDEX") (format "@tindex %s" value))
897 ((string= key "VINDEX") (format "@vindex %s" value)))))
899 ;;;; Line Break
901 (defun org-texinfo-line-break (line-break contents info)
902 "Transcode a LINE-BREAK object from Org to Texinfo.
903 CONTENTS is nil. INFO is a plist holding contextual information."
904 "@*\n")
906 ;;;; Link
908 (defun org-texinfo-link (link desc info)
909 "Transcode a LINK object from Org to Texinfo.
911 DESC is the description part of the link, or the empty string.
912 INFO is a plist holding contextual information. See
913 `org-export-data'."
914 (let* ((type (org-element-property :type link))
915 (raw-path (org-element-property :path link))
916 ;; Ensure DESC really exists, or set it to nil.
917 (desc (and (not (string= desc "")) desc))
918 (path (cond
919 ((member type '("http" "https" "ftp"))
920 (concat type ":" raw-path))
921 ((string= type "file") (org-export-file-uri raw-path))
922 (t raw-path))))
923 (cond
924 ((org-export-custom-protocol-maybe link desc 'texinfo))
925 ((org-export-inline-image-p link org-texinfo-inline-image-rules)
926 (org-texinfo--inline-image link info))
927 ((equal type "radio")
928 (let ((destination (org-export-resolve-radio-link link info)))
929 (if (not destination) desc
930 (format "@ref{%s,,%s}"
931 (org-texinfo--get-node destination info)
932 desc))))
933 ((member type '("custom-id" "id" "fuzzy"))
934 (let ((destination
935 (if (equal type "fuzzy")
936 (org-export-resolve-fuzzy-link link info)
937 (org-export-resolve-id-link link info))))
938 (case (org-element-type destination)
939 ((nil)
940 (format org-texinfo-link-with-unknown-path-format
941 (org-texinfo--sanitize-content path)))
942 ;; Id link points to an external file.
943 (plain-text
944 (if desc (format "@uref{file://%s,%s}" destination desc)
945 (format "@uref{file://%s}" destination)))
946 (headline
947 (format "@ref{%s,%s}"
948 (org-texinfo--get-node destination info)
949 (cond
950 (desc)
951 ((org-export-numbered-headline-p destination info)
952 (org-export-data
953 (org-element-property :title destination) info))
955 (mapconcat
956 #'number-to-string
957 (org-export-get-headline-number destination info) ".")))))
958 (otherwise
959 (let ((topic
960 (or desc
961 (if (and (eq (org-element-type destination) 'headline)
962 (not (org-export-numbered-headline-p
963 destination info)))
964 (org-export-data
965 (org-element-property :title destination) info))
966 (let ((n (org-export-get-ordinal destination info)))
967 (cond
968 ((not n) nil)
969 ((integerp n) n)
970 (t (mapconcat #'number-to-string n ".")))))))
971 (when topic
972 (format "@ref{%s,,%s}"
973 (org-texinfo--get-node destination info)
974 topic)))))))
975 ((equal type "info")
976 (let* ((info-path (split-string path "[:#]"))
977 (info-manual (car info-path))
978 (info-node (or (cadr info-path) "top"))
979 (title (or desc "")))
980 (format "@ref{%s,%s,,%s,}" info-node title info-manual)))
981 ((string= type "mailto")
982 (format "@email{%s}"
983 (concat (org-texinfo--sanitize-content path)
984 (and desc (concat "," desc)))))
985 ;; External link with a description part.
986 ((and path desc) (format "@uref{%s,%s}" path desc))
987 ;; External link without a description part.
988 (path (format "@uref{%s}" path))
989 ;; No path, only description. Try to do something useful.
991 (format (plist-get info :texinfo-link-with-unknown-path-format) desc)))))
993 (defun org-texinfo--inline-image (link info)
994 "Return Texinfo code for an inline image.
995 LINK is the link pointing to the inline image. INFO is the
996 current state of the export, as a plist."
997 (let* ((parent (org-export-get-parent-element link))
998 (caption (org-export-get-caption parent))
999 (shortcaption (org-export-get-caption parent t))
1000 (path (org-element-property :path link))
1001 (filename
1002 (file-name-sans-extension
1003 (if (file-name-absolute-p path) (expand-file-name path) path)))
1004 (extension (file-name-extension path))
1005 (attributes (org-export-read-attribute :attr_texinfo parent))
1006 (height (or (plist-get attributes :height) ""))
1007 (width (or (plist-get attributes :width) ""))
1008 (alt (or (plist-get attributes :alt) ""))
1009 (image (format "@image{%s,%s,%s,%s,%s}"
1010 filename width height alt extension)))
1011 (if (not (or caption shortcaption)) image
1012 (let* ((label (org-element-property :name parent))
1013 (backend
1014 (org-export-create-backend
1015 :parent 'texinfo
1016 :transcoders '((link . (lambda (object c i) c))
1017 (radio-target . (lambda (object c i) c))
1018 (target . ignore))))
1019 (short-backend
1020 (org-export-create-backend
1021 :parent 'texinfo
1022 :transcoders '((footnote-reference . ignore)
1023 (inline-src-block . ignore)
1024 (link . (lambda (object c i) c))
1025 (radio-target . (lambda (object c i) c))
1026 (target . ignore)
1027 (verbatim . ignore))))
1028 (shortcaption-str
1029 (if (and shortcaption caption)
1030 (format "@shortcaption{%s}\n"
1031 (org-export-data-with-backend
1032 shortcaption short-backend info))
1033 ""))
1034 (caption (org-export-data-with-backend
1035 (or caption shortcaption)
1036 (if (equal shortcaption-str "") short-backend backend)
1037 info)))
1038 (format "@float %s%s\n%s\n@caption{%s}\n%s@end float"
1039 (org-export-translate "Figure" :utf-8 info)
1040 (if label (concat "," label) "")
1041 image
1042 caption
1043 shortcaption-str)))))
1046 ;;;; Menu
1048 (defun org-texinfo-make-menu (scope info &optional master)
1049 "Create the menu for inclusion in the Texinfo document.
1051 SCOPE is a headline or a full parse tree. INFO is the
1052 communication channel, as a plist.
1054 When optional argument MASTER is non-nil, generate a master menu,
1055 including detailed node listing."
1056 (let ((menu (org-texinfo--build-menu scope info)))
1057 (when (org-string-nw-p menu)
1058 (org-element-normalize-string
1059 (format
1060 "@menu\n%s@end menu"
1061 (concat menu
1062 (when master
1063 (let ((detailmenu
1064 (org-texinfo--build-menu
1065 scope info
1066 (let ((toc-depth (plist-get info :with-toc)))
1067 (if (wholenump toc-depth) toc-depth
1068 org-texinfo-max-toc-depth)))))
1069 (when (org-string-nw-p detailmenu)
1070 (concat "\n@detailmenu\n"
1071 "--- The Detailed Node Listing ---\n\n"
1072 detailmenu
1073 "@end detailmenu\n"))))))))))
1075 (defun org-texinfo--build-menu (scope info &optional level)
1076 "Build menu for entries within SCOPE.
1077 SCOPE is a headline or a full parse tree. INFO is a plist
1078 containing contextual information. When optional argument LEVEL
1079 is an integer, build the menu recursively, down to this depth."
1080 (cond
1081 ((not level)
1082 (org-texinfo--format-entries (org-texinfo--menu-entries scope info) info))
1083 ((zerop level) nil)
1085 (org-element-normalize-string
1086 (mapconcat
1087 (lambda (h)
1088 (let ((entries (org-texinfo--menu-entries h info)))
1089 (when entries
1090 (concat
1091 (format "%s\n\n%s\n"
1092 (org-export-data (org-export-get-alt-title h info) info)
1093 (org-texinfo--format-entries entries info))
1094 (org-texinfo--build-menu h info (1- level))))))
1095 (org-texinfo--menu-entries scope info) "")))))
1097 (defun org-texinfo--format-entries (entries info)
1098 "Format all direct menu entries in SCOPE, as a string.
1099 SCOPE is either a headline or a full Org document. INFO is
1100 a plist containing contextual information."
1101 (org-element-normalize-string
1102 (mapconcat
1103 (lambda (h)
1104 (let* ((title (org-export-data
1105 (org-export-get-alt-title h info) info))
1106 (node (org-texinfo--get-node h info))
1107 (entry (concat "* " title ":"
1108 (if (string= title node) ":"
1109 (concat " " node ". "))))
1110 (desc (org-element-property :DESCRIPTION h)))
1111 (if (not desc) entry
1112 (format (format "%%-%ds %%s" org-texinfo-node-description-column)
1113 entry desc))))
1114 entries "\n")))
1116 (defun org-texinfo--menu-entries (scope info)
1117 "List direct children in SCOPE needing a menu entry.
1118 SCOPE is a headline or a full parse tree. INFO is a plist
1119 holding contextual information."
1120 (let* ((cache (or (plist-get info :texinfo-entries-cache)
1121 (plist-get (plist-put info :texinfo-entries-cache
1122 (make-hash-table :test #'eq))
1123 :texinfo-entries-cache)))
1124 (cached-entries (gethash scope cache 'no-cache)))
1125 (if (not (eq cached-entries 'no-cache)) cached-entries
1126 (puthash scope
1127 (org-element-map (org-element-contents scope) 'headline
1128 (lambda (h)
1129 (and (not (org-not-nil (org-element-property :COPYING h)))
1130 (not (org-element-property :footnote-section-p h))
1131 (not (org-export-low-level-p h info))
1133 info nil 'headline)
1134 cache))))
1136 ;;;; Node Property
1138 (defun org-texinfo-node-property (node-property contents info)
1139 "Transcode a NODE-PROPERTY element from Org to Texinfo.
1140 CONTENTS is nil. INFO is a plist holding contextual
1141 information."
1142 (format "%s:%s"
1143 (org-element-property :key node-property)
1144 (let ((value (org-element-property :value node-property)))
1145 (if value (concat " " value) ""))))
1147 ;;;; Paragraph
1149 (defun org-texinfo-paragraph (paragraph contents info)
1150 "Transcode a PARAGRAPH element from Org to Texinfo.
1151 CONTENTS is the contents of the paragraph, as a string. INFO is
1152 the plist used as a communication channel."
1153 contents)
1155 ;;;; Plain List
1157 (defun org-texinfo-plain-list (plain-list contents info)
1158 "Transcode a PLAIN-LIST element from Org to Texinfo.
1159 CONTENTS is the contents of the list. INFO is a plist holding
1160 contextual information."
1161 (let* ((attr (org-export-read-attribute :attr_texinfo plain-list))
1162 (indic (or (plist-get attr :indic)
1163 (plist-get info :texinfo-def-table-markup)))
1164 (table-type (plist-get attr :table-type))
1165 (type (org-element-property :type plain-list))
1166 (list-type (cond
1167 ((eq type 'ordered) "enumerate")
1168 ((eq type 'unordered) "itemize")
1169 ((member table-type '("ftable" "vtable")) table-type)
1170 (t "table"))))
1171 (format "@%s\n%s@end %s"
1172 (if (eq type 'descriptive) (concat list-type " " indic) list-type)
1173 contents
1174 list-type)))
1176 ;;;; Plain Text
1178 (defun org-texinfo-plain-text (text info)
1179 "Transcode a TEXT string from Org to Texinfo.
1180 TEXT is the string to transcode. INFO is a plist holding
1181 contextual information."
1182 ;; First protect @, { and }.
1183 (let ((output (org-texinfo--sanitize-content text)))
1184 ;; Activate smart quotes. Be sure to provide original TEXT string
1185 ;; since OUTPUT may have been modified.
1186 (when (plist-get info :with-smart-quotes)
1187 (setq output
1188 (org-export-activate-smart-quotes output :texinfo info text)))
1189 ;; LaTeX into @LaTeX{} and TeX into @TeX{}
1190 (let ((case-fold-search nil)
1191 (start 0))
1192 (while (string-match "\\(\\(?:La\\)?TeX\\)" output start)
1193 (setq output (replace-match
1194 (format "@%s{}" (match-string 1 output)) nil t output)
1195 start (match-end 0))))
1196 ;; Convert special strings.
1197 (when (plist-get info :with-special-strings)
1198 (while (string-match (regexp-quote "...") output)
1199 (setq output (replace-match "@dots{}" nil t output))))
1200 ;; Handle break preservation if required.
1201 (when (plist-get info :preserve-breaks)
1202 (setq output (replace-regexp-in-string
1203 "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" output)))
1204 ;; Return value.
1205 output))
1207 ;;;; Planning
1209 (defun org-texinfo-planning (planning contents info)
1210 "Transcode a PLANNING element from Org to Texinfo.
1211 CONTENTS is nil. INFO is a plist holding contextual
1212 information."
1213 (concat
1214 "@noindent"
1215 (mapconcat
1216 'identity
1217 (delq nil
1218 (list
1219 (let ((closed (org-element-property :closed planning)))
1220 (when closed
1221 (concat
1222 (format "@strong{%s} " org-closed-string)
1223 (format (plist-get info :texinfo-inactive-timestamp-format)
1224 (org-timestamp-translate closed)))))
1225 (let ((deadline (org-element-property :deadline planning)))
1226 (when deadline
1227 (concat
1228 (format "@strong{%s} " org-deadline-string)
1229 (format (plist-get info :texinfo-active-timestamp-format)
1230 (org-timestamp-translate deadline)))))
1231 (let ((scheduled (org-element-property :scheduled planning)))
1232 (when scheduled
1233 (concat
1234 (format "@strong{%s} " org-scheduled-string)
1235 (format (plist-get info :texinfo-active-timestamp-format)
1236 (org-timestamp-translate scheduled)))))))
1237 " ")
1238 "@*"))
1240 ;;;; Property Drawer
1242 (defun org-texinfo-property-drawer (property-drawer contents info)
1243 "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
1244 CONTENTS holds the contents of the drawer. INFO is a plist
1245 holding contextual information."
1246 (and (org-string-nw-p contents)
1247 (format "@verbatim\n%s@end verbatim" contents)))
1249 ;;;; Quote Block
1251 (defun org-texinfo-quote-block (quote-block contents info)
1252 "Transcode a QUOTE-BLOCK element from Org to Texinfo.
1253 CONTENTS holds the contents of the block. INFO is a plist
1254 holding contextual information."
1255 (let* ((title (org-element-property :name quote-block))
1256 (start-quote (concat "@quotation"
1257 (if title
1258 (format " %s" title)))))
1259 (format "%s\n%s@end quotation" start-quote contents)))
1261 ;;;; Radio Target
1263 (defun org-texinfo-radio-target (radio-target text info)
1264 "Transcode a RADIO-TARGET object from Org to Texinfo.
1265 TEXT is the text of the target. INFO is a plist holding
1266 contextual information."
1267 (format "@anchor{%s}%s"
1268 (org-export-get-reference radio-target info)
1269 text))
1271 ;;;; Section
1273 (defun org-texinfo-section (section contents info)
1274 "Transcode a SECTION element from Org to Texinfo.
1275 CONTENTS holds the contents of the section. INFO is a plist
1276 holding contextual information."
1277 (concat contents
1278 (let ((parent (org-export-get-parent-headline section)))
1279 (and parent (org-texinfo-make-menu parent info)))))
1281 ;;;; Special Block
1283 (defun org-texinfo-special-block (special-block contents info)
1284 "Transcode a SPECIAL-BLOCK element from Org to Texinfo.
1285 CONTENTS holds the contents of the block. INFO is a plist used
1286 as a communication channel."
1287 (let ((type (org-element-property :type special-block)))
1288 (format "@%s\n%s@end %s" type contents type)))
1290 ;;;; Src Block
1292 (defun org-texinfo-src-block (src-block contents info)
1293 "Transcode a SRC-BLOCK element from Org to Texinfo.
1294 CONTENTS holds the contents of the item. INFO is a plist holding
1295 contextual information."
1296 (let ((lispp (org-string-match-p "lisp"
1297 (org-element-property :language src-block)))
1298 (code (org-texinfo--sanitize-content
1299 (org-export-format-code-default src-block info))))
1300 (format (if lispp "@lisp\n%s@end lisp" "@example\n%s@end example") code)))
1302 ;;;; Statistics Cookie
1304 (defun org-texinfo-statistics-cookie (statistics-cookie contents info)
1305 "Transcode a STATISTICS-COOKIE object from Org to Texinfo.
1306 CONTENTS is nil. INFO is a plist holding contextual information."
1307 (org-element-property :value statistics-cookie))
1309 ;;;; Subscript
1311 (defun org-texinfo-subscript (subscript contents info)
1312 "Transcode a SUBSCRIPT object from Org to Texinfo.
1313 CONTENTS is the contents of the object. INFO is a plist holding
1314 contextual information."
1315 (format "@math{_%s}" contents))
1317 ;;;; Superscript
1319 (defun org-texinfo-superscript (superscript contents info)
1320 "Transcode a SUPERSCRIPT object from Org to Texinfo.
1321 CONTENTS is the contents of the object. INFO is a plist holding
1322 contextual information."
1323 (format "@math{^%s}" contents))
1325 ;;;; Table
1327 (defun org-texinfo-table (table contents info)
1328 "Transcode a TABLE element from Org to Texinfo.
1329 CONTENTS is the contents of the table. INFO is a plist holding
1330 contextual information."
1331 (if (eq (org-element-property :type table) 'table.el)
1332 (format "@verbatim\n%s@end verbatim"
1333 (org-element-normalize-string
1334 (org-element-property :value table)))
1335 (let* ((col-width (org-export-read-attribute :attr_texinfo table :columns))
1336 (columns
1337 (if col-width (format "@columnfractions %s" col-width)
1338 (org-texinfo-table-column-widths table info))))
1339 (format "@multitable %s\n%s@end multitable"
1340 columns
1341 contents))))
1343 (defun org-texinfo-table-column-widths (table info)
1344 "Determine the largest table cell in each column to process alignment.
1345 TABLE is the table element to transcode. INFO is a plist used as
1346 a communication channel."
1347 (let ((widths (make-vector (cdr (org-export-table-dimensions table info)) 0)))
1348 (org-element-map table 'table-row
1349 (lambda (row)
1350 (let ((idx 0))
1351 (org-element-map row 'table-cell
1352 (lambda (cell)
1353 ;; Length of the cell in the original buffer is only an
1354 ;; approximation of the length of the cell in the
1355 ;; output. It can sometimes fail (e.g. it considers
1356 ;; "/a/" being larger than "ab").
1357 (let ((w (- (org-element-property :contents-end cell)
1358 (org-element-property :contents-begin cell))))
1359 (aset widths idx (max w (aref widths idx))))
1360 (incf idx))
1361 info)))
1362 info)
1363 (format "{%s}" (mapconcat (lambda (w) (make-string w ?a)) widths "} {"))))
1365 ;;;; Table Cell
1367 (defun org-texinfo-table-cell (table-cell contents info)
1368 "Transcode a TABLE-CELL element from Org to Texinfo.
1369 CONTENTS is the cell contents. INFO is a plist used as
1370 a communication channel."
1371 (concat
1372 (let ((scientific-notation
1373 (plist-get info :texinfo-table-scientific-notation)))
1374 (if (and contents
1375 scientific-notation
1376 (string-match orgtbl-exp-regexp contents))
1377 ;; Use appropriate format string for scientific notation.
1378 (format scientific-notation
1379 (match-string 1 contents)
1380 (match-string 2 contents))
1381 contents))
1382 (when (org-export-get-next-element table-cell info) "\n@tab ")))
1384 ;;;; Table Row
1386 (defun org-texinfo-table-row (table-row contents info)
1387 "Transcode a TABLE-ROW element from Org to Texinfo.
1388 CONTENTS is the contents of the row. INFO is a plist used as
1389 a communication channel."
1390 ;; Rules are ignored since table separators are deduced from
1391 ;; borders of the current row.
1392 (when (eq (org-element-property :type table-row) 'standard)
1393 (let ((rowgroup-tag
1394 (if (and (= 1 (org-export-table-row-group table-row info))
1395 (org-export-table-has-header-p
1396 (org-export-get-parent-table table-row) info))
1397 "@headitem "
1398 "@item ")))
1399 (concat rowgroup-tag contents "\n"))))
1401 ;;;; Target
1403 (defun org-texinfo-target (target contents info)
1404 "Transcode a TARGET object from Org to Texinfo.
1405 CONTENTS is nil. INFO is a plist holding contextual
1406 information."
1407 (format "@anchor{%s}" (org-export-get-reference target info)))
1409 ;;;; Timestamp
1411 (defun org-texinfo-timestamp (timestamp contents info)
1412 "Transcode a TIMESTAMP object from Org to Texinfo.
1413 CONTENTS is nil. INFO is a plist holding contextual
1414 information."
1415 (let ((value (org-texinfo-plain-text
1416 (org-timestamp-translate timestamp) info)))
1417 (case (org-element-property :type timestamp)
1418 ((active active-range)
1419 (format (plist-get info :texinfo-active-timestamp-format) value))
1420 ((inactive inactive-range)
1421 (format (plist-get info :texinfo-inactive-timestamp-format) value))
1422 (t (format (plist-get info :texinfo-diary-timestamp-format) value)))))
1424 ;;;; Verbatim
1426 (defun org-texinfo-verbatim (verbatim contents info)
1427 "Transcode a VERBATIM object from Org to Texinfo.
1428 CONTENTS is nil. INFO is a plist used as a communication
1429 channel."
1430 (org-texinfo--text-markup
1431 (org-element-property :value verbatim) 'verbatim info))
1433 ;;;; Verse Block
1435 (defun org-texinfo-verse-block (verse-block contents info)
1436 "Transcode a VERSE-BLOCK element from Org to Texinfo.
1437 CONTENTS is verse block contents. INFO is a plist holding
1438 contextual information."
1439 (format "@display\n%s@end display" contents))
1442 ;;; Interactive functions
1444 (defun org-texinfo-export-to-texinfo
1445 (&optional async subtreep visible-only body-only ext-plist)
1446 "Export current buffer to a Texinfo file.
1448 If narrowing is active in the current buffer, only export its
1449 narrowed part.
1451 If a region is active, export that region.
1453 A non-nil optional argument ASYNC means the process should happen
1454 asynchronously. The resulting file should be accessible through
1455 the `org-export-stack' interface.
1457 When optional argument SUBTREEP is non-nil, export the sub-tree
1458 at point, extracting information from the headline properties
1459 first.
1461 When optional argument VISIBLE-ONLY is non-nil, don't export
1462 contents of hidden elements.
1464 When optional argument BODY-ONLY is non-nil, only write code
1465 between \"\\begin{document}\" and \"\\end{document}\".
1467 EXT-PLIST, when provided, is a property list with external
1468 parameters overriding Org default settings, but still inferior to
1469 file-local settings.
1471 Return output file's name."
1472 (interactive)
1473 (let ((outfile (org-export-output-file-name ".texi" subtreep))
1474 (org-export-coding-system org-texinfo-coding-system))
1475 (org-export-to-file 'texinfo outfile
1476 async subtreep visible-only body-only ext-plist)))
1478 (defun org-texinfo-export-to-info
1479 (&optional async subtreep visible-only body-only ext-plist)
1480 "Export current buffer to Texinfo then process through to INFO.
1482 If narrowing is active in the current buffer, only export its
1483 narrowed part.
1485 If a region is active, export that region.
1487 A non-nil optional argument ASYNC means the process should happen
1488 asynchronously. The resulting file should be accessible through
1489 the `org-export-stack' interface.
1491 When optional argument SUBTREEP is non-nil, export the sub-tree
1492 at point, extracting information from the headline properties
1493 first.
1495 When optional argument VISIBLE-ONLY is non-nil, don't export
1496 contents of hidden elements.
1498 When optional argument BODY-ONLY is non-nil, only write code
1499 between \"\\begin{document}\" and \"\\end{document}\".
1501 EXT-PLIST, when provided, is a property list with external
1502 parameters overriding Org default settings, but still inferior to
1503 file-local settings.
1505 When optional argument PUB-DIR is set, use it as the publishing
1506 directory.
1508 Return INFO file's name."
1509 (interactive)
1510 (let ((outfile (org-export-output-file-name ".texi" subtreep))
1511 (org-export-coding-system org-texinfo-coding-system))
1512 (org-export-to-file 'texinfo outfile
1513 async subtreep visible-only body-only ext-plist
1514 (lambda (file) (org-texinfo-compile file)))))
1516 ;;;###autoload
1517 (defun org-texinfo-publish-to-texinfo (plist filename pub-dir)
1518 "Publish an org file to Texinfo.
1520 FILENAME is the filename of the Org file to be published. PLIST
1521 is the property list for the given project. PUB-DIR is the
1522 publishing directory.
1524 Return output file name."
1525 (org-publish-org-to 'texinfo filename ".texi" plist pub-dir))
1527 ;;;###autoload
1528 (defun org-texinfo-convert-region-to-texinfo ()
1529 "Assume the current region has org-mode syntax, and convert it to Texinfo.
1530 This can be used in any buffer. For example, you can write an
1531 itemized list in org-mode syntax in an Texinfo buffer and use
1532 this command to convert it."
1533 (interactive)
1534 (org-export-replace-region-by 'texinfo))
1536 (defun org-texinfo-compile (file)
1537 "Compile a texinfo file.
1539 FILE is the name of the file being compiled. Processing is
1540 done through the command specified in `org-texinfo-info-process'.
1542 Return INFO file name or an error if it couldn't be produced."
1543 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1544 (full-name (file-truename file))
1545 (out-dir (file-name-directory file))
1546 ;; Properly set working directory for compilation.
1547 (default-directory (if (file-name-absolute-p file)
1548 (file-name-directory full-name)
1549 default-directory))
1550 errors)
1551 (message (format "Processing Texinfo file %s..." file))
1552 (save-window-excursion
1553 ;; Replace %b, %f and %o with appropriate values in each command
1554 ;; before applying it. Output is redirected to "*Org INFO
1555 ;; Texinfo Output*" buffer.
1556 (let ((outbuf (get-buffer-create "*Org INFO Texinfo Output*")))
1557 (dolist (command org-texinfo-info-process)
1558 (shell-command
1559 (replace-regexp-in-string
1560 "%b" (shell-quote-argument base-name)
1561 (replace-regexp-in-string
1562 "%f" (shell-quote-argument full-name)
1563 (replace-regexp-in-string
1564 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1565 outbuf))
1566 ;; Collect standard errors from output buffer.
1567 (setq errors (org-texinfo-collect-errors outbuf)))
1568 (let ((infofile (concat out-dir base-name ".info")))
1569 ;; Check for process failure. Provide collected errors if
1570 ;; possible.
1571 (if (not (file-exists-p infofile))
1572 (error (concat (format "INFO file %s wasn't produced" infofile)
1573 (when errors (concat ": " errors))))
1574 ;; Else remove log files, when specified, and signal end of
1575 ;; process to user, along with any error encountered.
1576 (when org-texinfo-remove-logfiles
1577 (dolist (ext org-texinfo-logfiles-extensions)
1578 (let ((file (concat out-dir base-name "." ext)))
1579 (when (file-exists-p file) (delete-file file)))))
1580 (message (concat "Process completed"
1581 (if (not errors) "."
1582 (concat " with errors: " errors)))))
1583 ;; Return output file name.
1584 infofile))))
1586 (defun org-texinfo-collect-errors (buffer)
1587 "Collect some kind of errors from \"makeinfo\" command output.
1589 BUFFER is the buffer containing output.
1591 Return collected error types as a string, or nil if there was
1592 none."
1593 (with-current-buffer buffer
1594 (save-excursion
1595 (goto-char (point-min))
1596 ;; Find final "makeinfo" run.
1597 (when t
1598 (let ((case-fold-search t)
1599 (errors ""))
1600 (when (save-excursion
1601 (re-search-forward "perhaps incorrect sectioning?" nil t))
1602 (setq errors (concat errors " [incorrect sectioning]")))
1603 (when (save-excursion
1604 (re-search-forward "missing close brace" nil t))
1605 (setq errors (concat errors " [syntax error]")))
1606 (when (save-excursion
1607 (re-search-forward "Unknown command" nil t))
1608 (setq errors (concat errors " [undefined @command]")))
1609 (when (save-excursion
1610 (re-search-forward "No matching @end" nil t))
1611 (setq errors (concat errors " [block incomplete]")))
1612 (when (save-excursion
1613 (re-search-forward "requires a sectioning" nil t))
1614 (setq errors (concat errors " [invalid section command]")))
1615 (when (save-excursion
1616 (re-search-forward "\\[unexpected\]" nil t))
1617 (setq errors (concat errors " [unexpected error]")))
1618 (when (save-excursion
1619 (re-search-forward "misplaced " nil t))
1620 (setq errors (concat errors " [syntax error]")))
1621 (and (org-string-nw-p errors) (org-trim errors)))))))
1624 (provide 'ox-texinfo)
1626 ;; Local variables:
1627 ;; generated-autoload-file: "org-loaddefs.el"
1628 ;; End:
1630 ;;; ox-texinfo.el ends here