ox: Add #+SUBTITLE property in some backends
[org-mode/org-tableheadings.git] / lisp / ox-texinfo.el
blob788cd400044d56462b99b68c0fd56319b316e32f
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 :options-alist
91 '((:texinfo-filename "TEXINFO_FILENAME" nil nil t)
92 (:texinfo-class "TEXINFO_CLASS" nil org-texinfo-default-class t)
93 (:texinfo-header "TEXINFO_HEADER" nil nil newline)
94 (:texinfo-post-header "TEXINFO_POST_HEADER" nil nil newline)
95 (:subtitle "SUBTITLE" nil nil parse)
96 (:subauthor "SUBAUTHOR" nil nil newline)
97 (:texinfo-dircat "TEXINFO_DIR_CATEGORY" nil nil t)
98 (:texinfo-dirtitle "TEXINFO_DIR_TITLE" nil nil t)
99 (:texinfo-dirdesc "TEXINFO_DIR_DESC" nil nil t)
100 (:texinfo-printed-title "TEXINFO_PRINTED_TITLE" nil nil t)
101 ;; Other variables.
102 (:texinfo-classes nil nil org-texinfo-classes)
103 (:texinfo-format-headline-function nil nil org-texinfo-format-headline-function)
104 (:texinfo-node-description-column nil nil org-texinfo-node-description-column)
105 (:texinfo-active-timestamp-format nil nil org-texinfo-active-timestamp-format)
106 (:texinfo-inactive-timestamp-format nil nil org-texinfo-inactive-timestamp-format)
107 (:texinfo-diary-timestamp-format nil nil org-texinfo-diary-timestamp-format)
108 (:texinfo-link-with-unknown-path-format nil nil org-texinfo-link-with-unknown-path-format)
109 (:texinfo-tables-verbatim nil nil org-texinfo-tables-verbatim)
110 (:texinfo-table-scientific-notation nil nil org-texinfo-table-scientific-notation)
111 (:texinfo-def-table-markup nil nil org-texinfo-def-table-markup)
112 (:texinfo-text-markup-alist nil nil org-texinfo-text-markup-alist)
113 (:texinfo-format-drawer-function nil nil org-texinfo-format-drawer-function)
114 (:texinfo-format-inlinetask-function nil nil org-texinfo-format-inlinetask-function)))
118 ;;; User Configurable Variables
120 (defgroup org-export-texinfo nil
121 "Options for exporting Org mode files to Texinfo."
122 :tag "Org Export Texinfo"
123 :version "24.4"
124 :package-version '(Org . "8.0")
125 :group 'org-export)
127 ;;;; Preamble
129 (defcustom org-texinfo-coding-system nil
130 "Default document encoding for Texinfo output.
132 If `nil' it will default to `buffer-file-coding-system'."
133 :group 'org-export-texinfo
134 :type 'coding-system)
136 (defcustom org-texinfo-default-class "info"
137 "The default Texinfo class."
138 :group 'org-export-texinfo
139 :type '(string :tag "Texinfo class"))
141 (defcustom org-texinfo-classes
142 '(("info"
143 "@documentencoding AUTO\n@documentlanguage AUTO"
144 ("@chapter %s" . "@unnumbered %s")
145 ("@section %s" . "@unnumberedsec %s")
146 ("@subsection %s" . "@unnumberedsubsec %s")
147 ("@subsubsection %s" . "@unnumberedsubsubsec %s")))
148 "Alist of Texinfo classes and associated header and structure.
149 If #+TEXINFO_CLASS is set in the buffer, use its value and the
150 associated information. Here is the structure of each cell:
152 \(class-name
153 header-string
154 \(numbered-section . unnumbered-section)
155 ...)
158 The header string
159 -----------------
161 The header string is inserted in the header of the generated
162 document, right after \"@setfilename\" and \"@settitle\"
163 commands.
165 If it contains the special string
167 \"@documentencoding AUTO\"
169 \"AUTO\" will be replaced with an appropriate coding system. See
170 `org-texinfo-coding-system' for more information. Likewise, if
171 the string contains the special string
173 \"@documentlanguage AUTO\"
175 \"AUTO\" will be replaced with the language defined in the
176 buffer, through #+LANGUAGE keyword, or globally, with
177 `org-export-default-language', which see.
180 The sectioning structure
181 ------------------------
183 The sectioning structure of the class is given by the elements
184 following the header string. For each sectioning level, a number
185 of strings is specified. A %s formatter is mandatory in each
186 section string and will be replaced by the title of the section.
188 Instead of a list of sectioning commands, you can also specify
189 a function name. That function will be called with two
190 parameters, the reduced) level of the headline, and a predicate
191 non-nil when the headline should be numbered. It must return
192 a format string in which the section title will be added."
193 :group 'org-export-texinfo
194 :version "24.4"
195 :package-version '(Org . "8.2")
196 :type '(repeat
197 (list (string :tag "Texinfo class")
198 (string :tag "Texinfo header")
199 (repeat :tag "Levels" :inline t
200 (choice
201 (cons :tag "Heading"
202 (string :tag " numbered")
203 (string :tag "unnumbered"))
204 (function :tag "Hook computing sectioning"))))))
206 ;;;; Headline
208 (defcustom org-texinfo-format-headline-function
209 'org-texinfo-format-headline-default-function
210 "Function to format headline text.
212 This function will be called with 5 arguments:
213 TODO the todo keyword (string or nil).
214 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
215 PRIORITY the priority of the headline (integer or nil)
216 TEXT the main headline text (string).
217 TAGS the tags as a list of strings (list of strings or nil).
219 The function result will be used in the section format string."
220 :group 'org-export-texinfo
221 :type 'function
222 :version "25.1"
223 :package-version '(Org . "8.3"))
225 ;;;; Node listing (menu)
227 (defcustom org-texinfo-node-description-column 32
228 "Column at which to start the description in the node listings.
229 If a node title is greater than this length, the description will
230 be placed after the end of the title."
231 :group 'org-export-texinfo
232 :type 'integer)
234 ;;;; Timestamps
236 (defcustom org-texinfo-active-timestamp-format "@emph{%s}"
237 "A printf format string to be applied to active timestamps."
238 :group 'org-export-texinfo
239 :type 'string)
241 (defcustom org-texinfo-inactive-timestamp-format "@emph{%s}"
242 "A printf format string to be applied to inactive timestamps."
243 :group 'org-export-texinfo
244 :type 'string)
246 (defcustom org-texinfo-diary-timestamp-format "@emph{%s}"
247 "A printf format string to be applied to diary timestamps."
248 :group 'org-export-texinfo
249 :type 'string)
251 ;;;; Links
253 (defcustom org-texinfo-link-with-unknown-path-format "@indicateurl{%s}"
254 "Format string for links with unknown path type."
255 :group 'org-export-texinfo
256 :type 'string)
258 ;;;; Tables
260 (defcustom org-texinfo-tables-verbatim nil
261 "When non-nil, tables are exported verbatim."
262 :group 'org-export-texinfo
263 :type 'boolean)
265 (defcustom org-texinfo-table-scientific-notation "%s\\,(%s)"
266 "Format string to display numbers in scientific notation.
267 The format should have \"%s\" twice, for mantissa and exponent
268 \(i.e. \"%s\\\\times10^{%s}\").
270 When nil, no transformation is made."
271 :group 'org-export-texinfo
272 :type '(choice
273 (string :tag "Format string")
274 (const :tag "No formatting" nil)))
276 (defcustom org-texinfo-def-table-markup "@samp"
277 "Default setting for @table environments."
278 :group 'org-export-texinfo
279 :type 'string)
281 ;;;; Text markup
283 (defcustom org-texinfo-text-markup-alist '((bold . "@strong{%s}")
284 (code . code)
285 (italic . "@emph{%s}")
286 (verbatim . verb)
287 (comment . "@c %s"))
288 "Alist of Texinfo expressions to convert text markup.
290 The key must be a symbol among `bold', `italic' and `comment'.
291 The value is a formatting string to wrap fontified text with.
293 Value can also be set to the following symbols: `verb' and
294 `code'. For the former, Org will use \"@verb\" to
295 create a format string and select a delimiter character that
296 isn't in the string. For the latter, Org will use \"@code\"
297 to typeset and try to protect special characters.
299 If no association can be found for a given markup, text will be
300 returned as-is."
301 :group 'org-export-texinfo
302 :type 'alist
303 :options '(bold code italic verbatim comment))
305 ;;;; Drawers
307 (defcustom org-texinfo-format-drawer-function
308 (lambda (name contents) contents)
309 "Function called to format a drawer in Texinfo code.
311 The function must accept two parameters:
312 NAME the drawer name, like \"LOGBOOK\"
313 CONTENTS the contents of the drawer.
315 The function should return the string to be exported.
317 The default function simply returns the value of CONTENTS."
318 :group 'org-export-texinfo
319 :version "24.4"
320 :package-version '(Org . "8.2")
321 :type 'function)
323 ;;;; Inlinetasks
325 (defcustom org-texinfo-format-inlinetask-function
326 'org-texinfo-format-inlinetask-default-function
327 "Function called to format an inlinetask in Texinfo code.
329 The function must accept six parameters:
330 TODO the todo keyword, as a string
331 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
332 PRIORITY the inlinetask priority, as a string
333 NAME the inlinetask name, as a string.
334 TAGS the inlinetask tags, as a list of strings.
335 CONTENTS the contents of the inlinetask, as a string.
337 The function should return the string to be exported."
338 :group 'org-export-texinfo
339 :type 'function)
341 ;;;; Compilation
343 (defcustom org-texinfo-info-process '("makeinfo %f")
344 "Commands to process a Texinfo file to an INFO file.
345 This is list of strings, each of them will be given to the shell
346 as a command. %f in the command will be replaced by the full
347 file name, %b by the file base name (i.e without extension) and
348 %o by the base directory of the file."
349 :group 'org-export-texinfo
350 :type '(repeat :tag "Shell command sequence"
351 (string :tag "Shell command")))
353 (defcustom org-texinfo-logfiles-extensions
354 '("aux" "toc" "cp" "fn" "ky" "pg" "tp" "vr")
355 "The list of file extensions to consider as Texinfo logfiles.
356 The logfiles will be remove if `org-texinfo-remove-logfiles' is
357 non-nil."
358 :group 'org-export-texinfo
359 :type '(repeat (string :tag "Extension")))
361 (defcustom org-texinfo-remove-logfiles t
362 "Non-nil means remove the logfiles produced by compiling a Texinfo file.
363 By default, logfiles are files with these extensions: .aux, .toc,
364 .cp, .fn, .ky, .pg and .tp. To define the set of logfiles to remove,
365 set `org-texinfo-logfiles-extensions'."
366 :group 'org-export-latex
367 :type 'boolean)
369 ;;; Constants
371 (defconst org-texinfo-max-toc-depth 4
372 "Maximum depth for creation of detailed menu listings.
373 Beyond this depth, Texinfo will not recognize the nodes and will
374 cause errors. Left as a constant in case this value ever
375 changes.")
377 (defconst org-texinfo-supported-coding-systems
378 '("US-ASCII" "UTF-8" "ISO-8859-15" "ISO-8859-1" "ISO-8859-2" "koi8-r" "koi8-u")
379 "List of coding systems supported by Texinfo, as strings.
380 Specified coding system will be matched against these strings.
381 If two strings share the same prefix (e.g. \"ISO-8859-1\" and
382 \"ISO-8859-15\"), the most specific one has to be listed first.")
385 ;;; Internal Functions
387 (defun org-texinfo-filter-section-blank-lines (headline back-end info)
388 "Filter controlling number of blank lines after a section."
389 (let ((blanks (make-string 2 ?\n)))
390 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline)))
392 (defun org-texinfo--normalize-headlines (tree back-end info)
393 "Normalize headlines in TREE.
395 BACK-END is the symbol specifying back-end used for export. INFO
396 is a plist used as a communication channel.
398 Make sure every headline in TREE contains a section, since those
399 are required to install a menu. Also put exactly one blank line
400 at the end of each section.
402 Return new tree."
403 (org-element-map tree 'headline
404 (lambda (hl)
405 (org-element-put-property hl :post-blank 1)
406 (let ((contents (org-element-contents hl)))
407 (when contents
408 (let ((first (org-element-map contents '(headline section)
409 #'identity info t)))
410 (unless (eq (org-element-type first) 'section)
411 (apply #'org-element-set-contents
413 (cons `(section (:parent ,hl)) contents)))))))
414 info)
415 tree)
417 (defun org-texinfo--find-verb-separator (s)
418 "Return a character not used in string S.
419 This is used to choose a separator for constructs like \\verb."
420 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
421 (loop for c across ll
422 when (not (string-match (regexp-quote (char-to-string c)) s))
423 return (char-to-string c))))
425 (defun org-texinfo--text-markup (text markup info)
426 "Format TEXT depending on MARKUP text markup.
427 INFO is a plist used as a communication channel. See
428 `org-texinfo-text-markup-alist' for details."
429 (let ((fmt (cdr (assq markup org-texinfo-text-markup-alist))))
430 (cond
431 ;; No format string: Return raw text.
432 ((not fmt) text)
433 ((eq 'verb fmt)
434 (let ((separator (org-texinfo--find-verb-separator text)))
435 (concat "@verb{" separator text separator "}")))
436 ((eq 'code fmt)
437 (let ((start 0)
438 (rtn "")
439 char)
440 (while (string-match "[@{}]" text)
441 (setq char (match-string 0 text))
442 (if (> (match-beginning 0) 0)
443 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
444 (setq text (substring text (1+ (match-beginning 0))))
445 (setq char (concat "@" char)
446 rtn (concat rtn char)))
447 (setq text (concat rtn text)
448 fmt "@code{%s}")
449 (format fmt text)))
450 ;; Else use format string.
451 (t (format fmt text)))))
453 (defun org-texinfo--get-node (blob info)
454 "Return node or anchor associated to BLOB.
455 BLOB is an element or object. INFO is a plist used as
456 a communication channel. The function guarantees the node or
457 anchor name is unique."
458 (let ((cache (plist-get info :texinfo-node-cache)))
459 (or (cdr (assq blob cache))
460 (let ((name
461 (org-texinfo--sanitize-node
462 (case (org-element-type blob)
463 (headline
464 (org-export-data (org-export-get-alt-title blob info) info))
465 ((radio-target target) (org-element-property :value blob))
466 (otherwise (or (org-element-property :name blob) ""))))))
467 ;; Ensure NAME is unique.
468 (while (rassoc name cache) (setq name (concat name "x")))
469 (plist-put info :texinfo-node-cache (cons (cons blob name) cache))
470 name))))
472 ;;;; Menu sanitizing
474 (defun org-texinfo--sanitize-node (title)
475 "Bend string TITLE to node line requirements.
476 Trim string and collapse multiple whitespace characters as they
477 are not significant. Also remove the following characters: @
478 { } ( ) : . ,"
479 (replace-regexp-in-string
480 "[:,.]" ""
481 (replace-regexp-in-string
482 "\\`(\\(.*)\\)" "[\\1"
483 (org-trim (replace-regexp-in-string "[ \t]\\{2,\\}" " " title)))))
485 ;;;; Content sanitizing
487 (defun org-texinfo--sanitize-content (text)
488 "Escape special characters in string TEXT.
489 Special characters are: @ { }"
490 (replace-regexp-in-string "[@{}]" "@\\&" text))
492 ;;; Template
494 (defun org-texinfo-template (contents info)
495 "Return complete document string after Texinfo conversion.
496 CONTENTS is the transcoded contents string. INFO is a plist
497 holding export options."
498 (let ((title (org-export-data (plist-get info :title) info))
499 ;; Copying data is the contents of the first headline in
500 ;; parse tree with a non-nil copying property.
501 (copying (org-element-map (plist-get info :parse-tree) 'headline
502 (lambda (hl)
503 (and (org-not-nil (org-element-property :COPYING hl))
504 (org-element-contents hl)))
505 info t)))
506 (concat
507 "\\input texinfo @c -*- texinfo -*-\n"
508 "@c %**start of header\n"
509 (let ((file (or (plist-get info :texinfo-filename)
510 (let ((f (plist-get info :output-file)))
511 (and f (concat (file-name-sans-extension f) ".info"))))))
512 (and file (format "@setfilename %s\n" file)))
513 (format "@settitle %s\n" title)
514 ;; Insert class-defined header.
515 (org-element-normalize-string
516 (let ((header (nth 1 (assoc (plist-get info :texinfo-class)
517 org-texinfo-classes)))
518 (coding
519 (catch 'coding-system
520 (let ((case-fold-search t)
521 (name (symbol-name (or org-texinfo-coding-system
522 buffer-file-coding-system))))
523 (dolist (system org-texinfo-supported-coding-systems "UTF-8")
524 (when (org-string-match-p (regexp-quote system) name)
525 (throw 'coding-system system))))))
526 (language (plist-get info :language))
527 (case-fold-search nil))
528 ;; Auto coding system.
529 (replace-regexp-in-string
530 "^@documentencoding \\(AUTO\\)$"
531 coding
532 (replace-regexp-in-string
533 "^@documentlanguage \\(AUTO\\)$" language header t nil 1) t nil 1)))
534 ;; Additional header options set by #+TEXINFO_HEADER.
535 (let ((texinfo-header (plist-get info :texinfo-header)))
536 (and texinfo-header (org-element-normalize-string texinfo-header)))
537 "@c %**end of header\n\n"
538 ;; Additional options set by #+TEXINFO_POST_HEADER.
539 (let ((texinfo-post-header (plist-get info :texinfo-post-header)))
540 (and texinfo-post-header
541 (org-element-normalize-string texinfo-post-header)))
542 ;; Copying.
543 (and copying
544 (format "@copying\n%s@end copying\n\n"
545 (org-element-normalize-string
546 (org-export-data copying info))))
547 ;; Info directory information. Only supply if both title and
548 ;; category are provided.
549 (let ((dircat (plist-get info :texinfo-dircat))
550 (dirtitle
551 (let ((title (plist-get info :texinfo-dirtitle)))
552 (and title
553 (string-match "^\\(?:\\* \\)?\\(.*?\\)\\(\\.\\)?$" title)
554 (format "* %s." (match-string 1 title))))))
555 (when (and dircat dirtitle)
556 (concat "@dircategory " dircat "\n"
557 "@direntry\n"
558 (let ((dirdesc
559 (let ((desc (plist-get info :texinfo-dirdesc)))
560 (cond ((not desc) nil)
561 ((org-string-match-p "\\.$" desc) desc)
562 (t (concat desc "."))))))
563 (if dirdesc (format "%-23s %s" dirtitle dirdesc) dirtitle))
564 "\n"
565 "@end direntry\n\n")))
566 ;; Title
567 "@finalout\n"
568 "@titlepage\n"
569 (when (plist-get info :with-title)
570 (concat
571 (format "@title %s\n" (or (plist-get info :texinfo-printed-title) title ""))
572 (let ((subtitle (plist-get info :subtitle)))
573 (when subtitle
574 (format "@subtitle %s\n"
575 (org-export-data subtitle info))))))
576 (when (plist-get info :with-author)
577 (concat
578 ;; Primary author.
579 (let ((author (org-string-nw-p
580 (org-export-data (plist-get info :author) info)))
581 (email (and (plist-get info :with-email)
582 (org-string-nw-p
583 (org-export-data (plist-get info :email) info)))))
584 (cond ((and author email)
585 (format "@author %s (@email{%s})\n" author email))
586 (author (format "@author %s\n" author))
587 (email (format "@author @email{%s}\n" email))))
588 ;; Other authors.
589 (let ((subauthor (plist-get info :subauthor)))
590 (and subauthor
591 (org-element-normalize-string
592 (replace-regexp-in-string "^" "@author " subauthor))))))
593 (and copying "@page\n@vskip 0pt plus 1filll\n@insertcopying\n")
594 "@end titlepage\n\n"
595 ;; Table of contents.
596 (and (plist-get info :with-toc) "@contents\n\n")
597 ;; Configure Top Node when not for Tex
598 "@ifnottex\n"
599 "@node Top\n"
600 (format "@top %s\n" title)
601 (and copying "@insertcopying\n")
602 "@end ifnottex\n\n"
603 ;; Menu.
604 (org-texinfo-make-menu (plist-get info :parse-tree) info 'master)
605 "\n"
606 ;; Document's body.
607 contents "\n"
608 ;; Creator.
609 (and (plist-get info :with-creator)
610 (concat (plist-get info :creator) "\n"))
611 ;; Document end.
612 "@bye")))
616 ;;; Transcode Functions
618 ;;;; Bold
620 (defun org-texinfo-bold (bold contents info)
621 "Transcode BOLD from Org to Texinfo.
622 CONTENTS is the text with bold markup. INFO is a plist holding
623 contextual information."
624 (org-texinfo--text-markup contents 'bold info))
626 ;;;; Center Block
628 (defun org-texinfo-center-block (center-block contents info)
629 "Transcode a CENTER-BLOCK element from Org to Texinfo.
630 CONTENTS holds the contents of the block. INFO is a plist used
631 as a communication channel."
632 contents)
634 ;;;; Clock
636 (defun org-texinfo-clock (clock contents info)
637 "Transcode a CLOCK element from Org to Texinfo.
638 CONTENTS is nil. INFO is a plist holding contextual
639 information."
640 (concat
641 "@noindent"
642 (format "@strong{%s} " org-clock-string)
643 (format (plist-get info :texinfo-inactive-timestamp-format)
644 (concat (org-timestamp-translate (org-element-property :value clock))
645 (let ((time (org-element-property :duration clock)))
646 (and time (format " (%s)" time)))))
647 "@*"))
649 ;;;; Code
651 (defun org-texinfo-code (code contents info)
652 "Transcode a CODE object from Org to Texinfo.
653 CONTENTS is nil. INFO is a plist used as a communication
654 channel."
655 (org-texinfo--text-markup (org-element-property :value code) 'code info))
657 ;;;; Drawer
659 (defun org-texinfo-drawer (drawer contents info)
660 "Transcode a DRAWER element from Org to Texinfo.
661 CONTENTS holds the contents of the block. INFO is a plist
662 holding contextual information."
663 (let* ((name (org-element-property :drawer-name drawer))
664 (output (funcall (plist-get info :texinfo-format-drawer-function)
665 name contents)))
666 output))
668 ;;;; Dynamic Block
670 (defun org-texinfo-dynamic-block (dynamic-block contents info)
671 "Transcode a DYNAMIC-BLOCK element from Org to Texinfo.
672 CONTENTS holds the contents of the block. INFO is a plist
673 holding contextual information."
674 contents)
676 ;;;; Entity
678 (defun org-texinfo-entity (entity contents info)
679 "Transcode an ENTITY object from Org to Texinfo.
680 CONTENTS are the definition itself. INFO is a plist holding
681 contextual information."
682 (let ((ent (org-element-property :latex entity)))
683 (if (org-element-property :latex-math-p entity) (format "@math{%s}" ent) ent)))
685 ;;;; Example Block
687 (defun org-texinfo-example-block (example-block contents info)
688 "Transcode an EXAMPLE-BLOCK element from Org to Texinfo.
689 CONTENTS is nil. INFO is a plist holding contextual
690 information."
691 (format "@verbatim\n%s@end verbatim"
692 (org-export-format-code-default example-block info)))
694 ;;; Export Block
696 (defun org-texinfo-export-block (export-block contents info)
697 "Transcode a EXPORT-BLOCK element from Org to Texinfo.
698 CONTENTS is nil. INFO is a plist holding contextual information."
699 (when (string= (org-element-property :type export-block) "TEXINFO")
700 (org-remove-indentation (org-element-property :value export-block))))
702 ;;; Export Snippet
704 (defun org-texinfo-export-snippet (export-snippet contents info)
705 "Transcode a EXPORT-SNIPPET object from Org to Texinfo.
706 CONTENTS is nil. INFO is a plist holding contextual information."
707 (when (eq (org-export-snippet-backend export-snippet) 'texinfo)
708 (org-element-property :value export-snippet)))
710 ;;;; Fixed Width
712 (defun org-texinfo-fixed-width (fixed-width contents info)
713 "Transcode a FIXED-WIDTH element from Org to Texinfo.
714 CONTENTS is nil. INFO is a plist holding contextual information."
715 (format "@example\n%s\n@end example"
716 (org-remove-indentation
717 (org-texinfo--sanitize-content
718 (org-element-property :value fixed-width)))))
720 ;;;; Footnote Reference
722 (defun org-texinfo-footnote-reference (footnote contents info)
723 "Create a footnote reference for FOOTNOTE.
725 FOOTNOTE is the footnote to define. CONTENTS is nil. INFO is a
726 plist holding contextual information."
727 (let ((def (org-export-get-footnote-definition footnote info)))
728 (format "@footnote{%s}"
729 (org-trim (org-export-data def info)))))
731 ;;;; Headline
733 (defun org-texinfo-headline (headline contents info)
734 "Transcode a HEADLINE element from Org to Texinfo.
735 CONTENTS holds the contents of the headline. INFO is a plist
736 holding contextual information."
737 (let* ((class (plist-get info :texinfo-class))
738 (level (org-export-get-relative-level headline info))
739 (numberedp (org-export-numbered-headline-p headline info))
740 (class-sectioning (assoc class (plist-get info :texinfo-classes)))
741 ;; Find the index type, if any.
742 (index (org-element-property :INDEX headline))
743 ;; Create node info, to insert it before section formatting.
744 ;; Use custom menu title if present.
745 (node (format "@node %s\n" (org-texinfo--get-node headline info)))
746 ;; Section formatting will set two placeholders: one for the
747 ;; title and the other for the contents.
748 (section-fmt
749 (if (org-not-nil (org-element-property :APPENDIX headline))
750 "@appendix %s\n%s"
751 (let ((sec (if (and (symbolp (nth 2 class-sectioning))
752 (fboundp (nth 2 class-sectioning)))
753 (funcall (nth 2 class-sectioning) level numberedp)
754 (nth (1+ level) class-sectioning))))
755 (cond
756 ;; No section available for that LEVEL.
757 ((not sec) nil)
758 ;; Section format directly returned by a function.
759 ((stringp sec) sec)
760 ;; (numbered-section . unnumbered-section)
761 ((not (consp (cdr sec)))
762 (concat (if (or index (not numberedp)) (cdr sec) (car sec))
763 "\n%s"))))))
764 (todo
765 (and (plist-get info :with-todo-keywords)
766 (let ((todo (org-element-property :todo-keyword headline)))
767 (and todo (org-export-data todo info)))))
768 (todo-type (and todo (org-element-property :todo-type headline)))
769 (tags (and (plist-get info :with-tags)
770 (org-export-get-tags headline info)))
771 (priority (and (plist-get info :with-priority)
772 (org-element-property :priority headline)))
773 (text (org-export-data (org-element-property :title headline) info))
774 (full-text (funcall (plist-get info :texinfo-format-headline-function)
775 todo todo-type priority text tags))
776 (contents (if (org-string-nw-p contents) (concat "\n" contents) "")))
777 (cond
778 ;; Case 1: This is a footnote section: ignore it.
779 ((org-element-property :footnote-section-p headline) nil)
780 ;; Case 2: This is the `copying' section: ignore it
781 ;; This is used elsewhere.
782 ((org-not-nil (org-element-property :COPYING headline)) nil)
783 ;; Case 3: An index. If it matches one of the known indexes,
784 ;; print it as such following the contents, otherwise
785 ;; print the contents and leave the index up to the user.
786 (index
787 (concat node
788 (format
789 section-fmt
790 full-text
791 (concat contents
792 (and (member index '("cp" "fn" "ky" "pg" "tp" "vr"))
793 (concat "\n@printindex " index))))))
794 ;; Case 4: This is a deep sub-tree: export it as a list item.
795 ;; Also export as items headlines for which no section
796 ;; format has been found.
797 ((or (not section-fmt) (org-export-low-level-p headline info))
798 ;; Build the real contents of the sub-tree.
799 (concat (and (org-export-first-sibling-p headline info)
800 (format "@%s\n" (if numberedp 'enumerate 'itemize)))
801 "@item\n" full-text "\n"
802 contents
803 (if (org-export-last-sibling-p headline info)
804 (format "@end %s" (if numberedp 'enumerate 'itemize))
805 "\n")))
806 ;; Case 5: Standard headline. Export it as a section.
807 (t (concat node (format section-fmt full-text contents))))))
809 (defun org-texinfo-format-headline-default-function
810 (todo todo-type priority text tags)
811 "Default format function for a headline.
812 See `org-texinfo-format-headline-function' for details."
813 (concat (when todo (format "@strong{%s} " todo))
814 (when priority (format "@emph{#%s} " priority))
815 text
816 (when tags (format " :%s:" (mapconcat 'identity tags ":")))))
818 ;;;; Inline Src Block
820 (defun org-texinfo-inline-src-block (inline-src-block contents info)
821 "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
822 CONTENTS holds the contents of the item. INFO is a plist holding
823 contextual information."
824 (let* ((code (org-element-property :value inline-src-block))
825 (separator (org-texinfo--find-verb-separator code)))
826 (concat "@verb{" separator code separator "}")))
828 ;;;; Inlinetask
830 (defun org-texinfo-inlinetask (inlinetask contents info)
831 "Transcode an INLINETASK element from Org to Texinfo.
832 CONTENTS holds the contents of the block. INFO is a plist
833 holding contextual information."
834 (let ((title (org-export-data (org-element-property :title inlinetask) info))
835 (todo (and (plist-get info :with-todo-keywords)
836 (let ((todo (org-element-property :todo-keyword inlinetask)))
837 (and todo (org-export-data todo info)))))
838 (todo-type (org-element-property :todo-type inlinetask))
839 (tags (and (plist-get info :with-tags)
840 (org-export-get-tags inlinetask info)))
841 (priority (and (plist-get info :with-priority)
842 (org-element-property :priority inlinetask))))
843 (funcall (plist-get info :texinfo-format-inlinetask-function)
844 todo todo-type priority title tags contents)))
846 (defun org-texinfo-format-inlinetask-default-function
847 (todo todo-type priority title tags contents)
848 "Default format function for a inlinetasks.
849 See `org-texinfo-format-inlinetask-function' for details."
850 (let ((full-title
851 (concat (when todo (format "@strong{%s} " todo))
852 (when priority (format "#%c " priority))
853 title
854 (when tags (format ":%s:" (mapconcat #'identity tags ":"))))))
855 (format "@center %s\n\n%s\n" full-title contents)))
857 ;;;; Italic
859 (defun org-texinfo-italic (italic contents info)
860 "Transcode ITALIC from Org to Texinfo.
861 CONTENTS is the text with italic markup. INFO is a plist holding
862 contextual information."
863 (org-texinfo--text-markup contents 'italic info))
865 ;;;; Item
867 (defun org-texinfo-item (item contents info)
868 "Transcode an ITEM element from Org to Texinfo.
869 CONTENTS holds the contents of the item. INFO is a plist holding
870 contextual information."
871 (format "@item%s\n%s"
872 (let ((tag (org-element-property :tag item)))
873 (if tag (concat " " (org-export-data tag info)) ""))
874 (or contents "")))
876 ;;;; Keyword
878 (defun org-texinfo-keyword (keyword contents info)
879 "Transcode a KEYWORD element from Org to Texinfo.
880 CONTENTS is nil. INFO is a plist holding contextual information."
881 (let ((key (org-element-property :key keyword))
882 (value (org-element-property :value keyword)))
883 (cond
884 ((string= key "TEXINFO") value)
885 ((string= key "CINDEX") (format "@cindex %s" value))
886 ((string= key "FINDEX") (format "@findex %s" value))
887 ((string= key "KINDEX") (format "@kindex %s" value))
888 ((string= key "PINDEX") (format "@pindex %s" value))
889 ((string= key "TINDEX") (format "@tindex %s" value))
890 ((string= key "VINDEX") (format "@vindex %s" value)))))
892 ;;;; Line Break
894 (defun org-texinfo-line-break (line-break contents info)
895 "Transcode a LINE-BREAK object from Org to Texinfo.
896 CONTENTS is nil. INFO is a plist holding contextual information."
897 "@*\n")
899 ;;;; Link
901 (defun org-texinfo-link (link desc info)
902 "Transcode a LINK object from Org to Texinfo.
904 DESC is the description part of the link, or the empty string.
905 INFO is a plist holding contextual information. See
906 `org-export-data'."
907 (let* ((type (org-element-property :type link))
908 (raw-path (org-element-property :path link))
909 ;; Ensure DESC really exists, or set it to nil.
910 (desc (and (not (string= desc "")) desc))
911 (path (cond
912 ((member type '("http" "https" "ftp"))
913 (concat type ":" raw-path))
914 ((and (string= type "file") (file-name-absolute-p raw-path))
915 (concat "file:" raw-path))
916 (t raw-path))))
917 (cond
918 ((org-export-custom-protocol-maybe link desc 'texinfo))
919 ((equal type "radio")
920 (let ((destination (org-export-resolve-radio-link link info)))
921 (if (not destination) desc
922 (format "@ref{%s,,%s}"
923 (org-texinfo--get-node destination info)
924 desc))))
925 ((member type '("custom-id" "id" "fuzzy"))
926 (let ((destination
927 (if (equal type "fuzzy")
928 (org-export-resolve-fuzzy-link link info)
929 (org-export-resolve-id-link link info))))
930 (case (org-element-type destination)
931 ((nil)
932 (format org-texinfo-link-with-unknown-path-format
933 (org-texinfo--sanitize-content path)))
934 ;; Id link points to an external file.
935 (plain-text
936 (if desc (format "@uref{file://%s,%s}" destination desc)
937 (format "@uref{file://%s}" destination)))
938 (headline
939 (format "@ref{%s,%s}"
940 (org-texinfo--get-node destination info)
941 (cond
942 (desc)
943 ((org-export-numbered-headline-p destination info)
944 (org-export-data
945 (org-element-property :title destination) info))
947 (mapconcat
948 #'number-to-string
949 (org-export-get-headline-number destination info) ".")))))
950 (otherwise
951 (let ((topic
952 (or desc
953 (if (and (eq (org-element-type destination) 'headline)
954 (not (org-export-numbered-headline-p
955 destination info)))
956 (org-export-data
957 (org-element-property :title destination) info))
958 (let ((n (org-export-get-ordinal destination info)))
959 (cond
960 ((not n) nil)
961 ((integerp n) n)
962 (t (mapconcat #'number-to-string n ".")))))))
963 (when topic
964 (format "@ref{%s,,%s}"
965 (org-texinfo--get-node destination info)
966 topic)))))))
967 ((equal type "info")
968 (let* ((info-path (split-string path "[:#]"))
969 (info-manual (car info-path))
970 (info-node (or (cadr info-path) "top"))
971 (title (or desc "")))
972 (format "@ref{%s,%s,,%s,}" info-node title info-manual)))
973 ((string= type "mailto")
974 (format "@email{%s}"
975 (concat (org-texinfo--sanitize-content path)
976 (and desc (concat "," desc)))))
977 ;; External link with a description part.
978 ((and path desc) (format "@uref{%s,%s}" path desc))
979 ;; External link without a description part.
980 (path (format "@uref{%s}" path))
981 ;; No path, only description. Try to do something useful.
983 (format (plist-get info :texinfo-link-with-unknown-path-format) desc)))))
986 ;;;; Menu
988 (defun org-texinfo-make-menu (scope info &optional master)
989 "Create the menu for inclusion in the Texinfo document.
991 SCOPE is a headline or a full parse tree. INFO is the
992 communication channel, as a plist.
994 When optional argument MASTER is non-nil, generate a master menu,
995 including detailed node listing."
996 (let ((menu (org-texinfo--build-menu scope info)))
997 (when (org-string-nw-p menu)
998 (org-element-normalize-string
999 (format
1000 "@menu\n%s@end menu"
1001 (concat menu
1002 (when master
1003 (let ((detailmenu
1004 (org-texinfo--build-menu
1005 scope info
1006 (let ((toc-depth (plist-get info :with-toc)))
1007 (if (wholenump toc-depth) toc-depth
1008 org-texinfo-max-toc-depth)))))
1009 (when (org-string-nw-p detailmenu)
1010 (concat "\n@detailmenu\n"
1011 "--- The Detailed Node Listing ---\n\n"
1012 detailmenu
1013 "@end detailmenu\n"))))))))))
1015 (defun org-texinfo--build-menu (scope info &optional level)
1016 "Build menu for entries within SCOPE.
1017 SCOPE is a headline or a full parse tree. INFO is a plist
1018 containing contextual information. When optional argument LEVEL
1019 is an integer, build the menu recursively, down to this depth."
1020 (cond
1021 ((not level)
1022 (org-texinfo--format-entries (org-texinfo--menu-entries scope info) info))
1023 ((zerop level) nil)
1025 (org-element-normalize-string
1026 (mapconcat
1027 (lambda (h)
1028 (let ((entries (org-texinfo--menu-entries h info)))
1029 (when entries
1030 (concat
1031 (format "%s\n\n%s\n"
1032 (org-export-data (org-export-get-alt-title h info) info)
1033 (org-texinfo--format-entries entries info))
1034 (org-texinfo--build-menu h info (1- level))))))
1035 (org-texinfo--menu-entries scope info) "")))))
1037 (defun org-texinfo--format-entries (entries info)
1038 "Format all direct menu entries in SCOPE, as a string.
1039 SCOPE is either a headline or a full Org document. INFO is
1040 a plist containing contextual information."
1041 (org-element-normalize-string
1042 (mapconcat
1043 (lambda (h)
1044 (let* ((title (org-export-data
1045 (org-export-get-alt-title h info) info))
1046 (node (org-texinfo--get-node h info))
1047 (entry (concat "* " title ":"
1048 (if (string= title node) ":"
1049 (concat " " node ". "))))
1050 (desc (org-element-property :DESCRIPTION h)))
1051 (if (not desc) entry
1052 (format (format "%%-%ds %%s" org-texinfo-node-description-column)
1053 entry desc))))
1054 entries "\n")))
1056 (defun org-texinfo--menu-entries (scope info)
1057 "List direct children in SCOPE needing a menu entry.
1058 SCOPE is a headline or a full parse tree. INFO is a plist
1059 holding contextual information."
1060 (let* ((cache (or (plist-get info :texinfo-entries-cache)
1061 (plist-get (plist-put info :texinfo-entries-cache
1062 (make-hash-table :test #'eq))
1063 :texinfo-entries-cache)))
1064 (cached-entries (gethash scope cache 'no-cache)))
1065 (if (not (eq cached-entries 'no-cache)) cached-entries
1066 (puthash scope
1067 (org-element-map (org-element-contents scope) 'headline
1068 (lambda (h)
1069 (and (not (org-not-nil (org-element-property :COPYING h)))
1070 (not (org-element-property :footnote-section-p h))
1071 (not (org-export-low-level-p h info))
1073 info nil 'headline)
1074 cache))))
1076 ;;;; Node Property
1078 (defun org-texinfo-node-property (node-property contents info)
1079 "Transcode a NODE-PROPERTY element from Org to Texinfo.
1080 CONTENTS is nil. INFO is a plist holding contextual
1081 information."
1082 (format "%s:%s"
1083 (org-element-property :key node-property)
1084 (let ((value (org-element-property :value node-property)))
1085 (if value (concat " " value) ""))))
1087 ;;;; Paragraph
1089 (defun org-texinfo-paragraph (paragraph contents info)
1090 "Transcode a PARAGRAPH element from Org to Texinfo.
1091 CONTENTS is the contents of the paragraph, as a string. INFO is
1092 the plist used as a communication channel."
1093 contents)
1095 ;;;; Plain List
1097 (defun org-texinfo-plain-list (plain-list contents info)
1098 "Transcode a PLAIN-LIST element from Org to Texinfo.
1099 CONTENTS is the contents of the list. INFO is a plist holding
1100 contextual information."
1101 (let* ((attr (org-export-read-attribute :attr_texinfo plain-list))
1102 (indic (or (plist-get attr :indic)
1103 (plist-get info :texinfo-def-table-markup)))
1104 (table-type (plist-get attr :table-type))
1105 (type (org-element-property :type plain-list))
1106 (list-type (cond
1107 ((eq type 'ordered) "enumerate")
1108 ((eq type 'unordered) "itemize")
1109 ((member table-type '("ftable" "vtable")) table-type)
1110 (t "table"))))
1111 (format "@%s\n%s@end %s"
1112 (if (eq type 'descriptive) (concat list-type " " indic) list-type)
1113 contents
1114 list-type)))
1116 ;;;; Plain Text
1118 (defun org-texinfo-plain-text (text info)
1119 "Transcode a TEXT string from Org to Texinfo.
1120 TEXT is the string to transcode. INFO is a plist holding
1121 contextual information."
1122 ;; First protect @, { and }.
1123 (let ((output (org-texinfo--sanitize-content text)))
1124 ;; Activate smart quotes. Be sure to provide original TEXT string
1125 ;; since OUTPUT may have been modified.
1126 (when (plist-get info :with-smart-quotes)
1127 (setq output
1128 (org-export-activate-smart-quotes output :texinfo info text)))
1129 ;; LaTeX into @LaTeX{} and TeX into @TeX{}
1130 (let ((case-fold-search nil)
1131 (start 0))
1132 (while (string-match "\\(\\(?:La\\)?TeX\\)" output start)
1133 (setq output (replace-match
1134 (format "@%s{}" (match-string 1 output)) nil t output)
1135 start (match-end 0))))
1136 ;; Convert special strings.
1137 (when (plist-get info :with-special-strings)
1138 (while (string-match (regexp-quote "...") output)
1139 (setq output (replace-match "@dots{}" nil t output))))
1140 ;; Handle break preservation if required.
1141 (when (plist-get info :preserve-breaks)
1142 (setq output (replace-regexp-in-string
1143 "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" output)))
1144 ;; Return value.
1145 output))
1147 ;;;; Planning
1149 (defun org-texinfo-planning (planning contents info)
1150 "Transcode a PLANNING element from Org to Texinfo.
1151 CONTENTS is nil. INFO is a plist holding contextual
1152 information."
1153 (concat
1154 "@noindent"
1155 (mapconcat
1156 'identity
1157 (delq nil
1158 (list
1159 (let ((closed (org-element-property :closed planning)))
1160 (when closed
1161 (concat
1162 (format "@strong{%s} " org-closed-string)
1163 (format (plist-get info :texinfo-inactive-timestamp-format)
1164 (org-timestamp-translate closed)))))
1165 (let ((deadline (org-element-property :deadline planning)))
1166 (when deadline
1167 (concat
1168 (format "@strong{%s} " org-deadline-string)
1169 (format (plist-get info :texinfo-active-timestamp-format)
1170 (org-timestamp-translate deadline)))))
1171 (let ((scheduled (org-element-property :scheduled planning)))
1172 (when scheduled
1173 (concat
1174 (format "@strong{%s} " org-scheduled-string)
1175 (format (plist-get info :texinfo-active-timestamp-format)
1176 (org-timestamp-translate scheduled)))))))
1177 " ")
1178 "@*"))
1180 ;;;; Property Drawer
1182 (defun org-texinfo-property-drawer (property-drawer contents info)
1183 "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
1184 CONTENTS holds the contents of the drawer. INFO is a plist
1185 holding contextual information."
1186 (and (org-string-nw-p contents)
1187 (format "@verbatim\n%s@end verbatim" contents)))
1189 ;;;; Quote Block
1191 (defun org-texinfo-quote-block (quote-block contents info)
1192 "Transcode a QUOTE-BLOCK element from Org to Texinfo.
1193 CONTENTS holds the contents of the block. INFO is a plist
1194 holding contextual information."
1195 (let* ((title (org-element-property :name quote-block))
1196 (start-quote (concat "@quotation"
1197 (if title
1198 (format " %s" title)))))
1199 (format "%s\n%s@end quotation" start-quote contents)))
1201 ;;;; Radio Target
1203 (defun org-texinfo-radio-target (radio-target text info)
1204 "Transcode a RADIO-TARGET object from Org to Texinfo.
1205 TEXT is the text of the target. INFO is a plist holding
1206 contextual information."
1207 (format "@anchor{%s}%s"
1208 (org-export-solidify-link-text
1209 (org-element-property :value radio-target))
1210 text))
1212 ;;;; Section
1214 (defun org-texinfo-section (section contents info)
1215 "Transcode a SECTION element from Org to Texinfo.
1216 CONTENTS holds the contents of the section. INFO is a plist
1217 holding contextual information."
1218 (concat contents
1219 (let ((parent (org-export-get-parent-headline section)))
1220 (and parent (org-texinfo-make-menu parent info)))))
1222 ;;;; Special Block
1224 (defun org-texinfo-special-block (special-block contents info)
1225 "Transcode a SPECIAL-BLOCK element from Org to Texinfo.
1226 CONTENTS holds the contents of the block. INFO is a plist used
1227 as a communication channel."
1228 (let ((type (org-element-property :type special-block)))
1229 (format "@%s\n%s@end %s" type contents type)))
1231 ;;;; Src Block
1233 (defun org-texinfo-src-block (src-block contents info)
1234 "Transcode a SRC-BLOCK element from Org to Texinfo.
1235 CONTENTS holds the contents of the item. INFO is a plist holding
1236 contextual information."
1237 (let ((lispp (org-string-match-p "lisp"
1238 (org-element-property :language src-block)))
1239 (code (org-texinfo--sanitize-content
1240 (org-export-format-code-default src-block info))))
1241 (format (if lispp "@lisp\n%s@end lisp" "@example\n%s@end example") code)))
1243 ;;;; Statistics Cookie
1245 (defun org-texinfo-statistics-cookie (statistics-cookie contents info)
1246 "Transcode a STATISTICS-COOKIE object from Org to Texinfo.
1247 CONTENTS is nil. INFO is a plist holding contextual information."
1248 (org-element-property :value statistics-cookie))
1250 ;;;; Subscript
1252 (defun org-texinfo-subscript (subscript contents info)
1253 "Transcode a SUBSCRIPT object from Org to Texinfo.
1254 CONTENTS is the contents of the object. INFO is a plist holding
1255 contextual information."
1256 (format "@math{_%s}" contents))
1258 ;;;; Superscript
1260 (defun org-texinfo-superscript (superscript contents info)
1261 "Transcode a SUPERSCRIPT object from Org to Texinfo.
1262 CONTENTS is the contents of the object. INFO is a plist holding
1263 contextual information."
1264 (format "@math{^%s}" contents))
1266 ;;;; Table
1268 (defun org-texinfo-table (table contents info)
1269 "Transcode a TABLE element from Org to Texinfo.
1270 CONTENTS is the contents of the table. INFO is a plist holding
1271 contextual information."
1272 (if (eq (org-element-property :type table) 'table.el)
1273 (format "@verbatim\n%s@end verbatim"
1274 (org-element-normalize-string
1275 (org-element-property :value table)))
1276 (let* ((col-width (org-export-read-attribute :attr_texinfo table :columns))
1277 (columns
1278 (if col-width (format "@columnfractions %s" col-width)
1279 (org-texinfo-table-column-widths table info))))
1280 (format "@multitable %s\n%s@end multitable"
1281 columns
1282 contents))))
1284 (defun org-texinfo-table-column-widths (table info)
1285 "Determine the largest table cell in each column to process alignment.
1286 TABLE is the table element to transcode. INFO is a plist used as
1287 a communication channel."
1288 (let ((widths (make-vector (cdr (org-export-table-dimensions table info)) 0)))
1289 (org-element-map table 'table-row
1290 (lambda (row)
1291 (let ((idx 0))
1292 (org-element-map row 'table-cell
1293 (lambda (cell)
1294 ;; Length of the cell in the original buffer is only an
1295 ;; approximation of the length of the cell in the
1296 ;; output. It can sometimes fail (e.g. it considers
1297 ;; "/a/" being larger than "ab").
1298 (let ((w (- (org-element-property :contents-end cell)
1299 (org-element-property :contents-begin cell))))
1300 (aset widths idx (max w (aref widths idx))))
1301 (incf idx))
1302 info)))
1303 info)
1304 (format "{%s}" (mapconcat (lambda (w) (make-string w ?a)) widths "} {"))))
1306 ;;;; Table Cell
1308 (defun org-texinfo-table-cell (table-cell contents info)
1309 "Transcode a TABLE-CELL element from Org to Texinfo.
1310 CONTENTS is the cell contents. INFO is a plist used as
1311 a communication channel."
1312 (concat
1313 (let ((scientific-notation
1314 (plist-get info :texinfo-table-scientific-notation)))
1315 (if (and contents
1316 scientific-notation
1317 (string-match orgtbl-exp-regexp contents))
1318 ;; Use appropriate format string for scientific notation.
1319 (format scientific-notation
1320 (match-string 1 contents)
1321 (match-string 2 contents))
1322 contents))
1323 (when (org-export-get-next-element table-cell info) "\n@tab ")))
1325 ;;;; Table Row
1327 (defun org-texinfo-table-row (table-row contents info)
1328 "Transcode a TABLE-ROW element from Org to Texinfo.
1329 CONTENTS is the contents of the row. INFO is a plist used as
1330 a communication channel."
1331 ;; Rules are ignored since table separators are deduced from
1332 ;; borders of the current row.
1333 (when (eq (org-element-property :type table-row) 'standard)
1334 (let ((rowgroup-tag
1335 (if (and (= 1 (org-export-table-row-group table-row info))
1336 (org-export-table-has-header-p
1337 (org-export-get-parent-table table-row) info))
1338 "@headitem "
1339 "@item ")))
1340 (concat rowgroup-tag contents "\n"))))
1342 ;;;; Target
1344 (defun org-texinfo-target (target contents info)
1345 "Transcode a TARGET object from Org to Texinfo.
1346 CONTENTS is nil. INFO is a plist holding contextual
1347 information."
1348 (format "@anchor{%s}"
1349 (org-export-solidify-link-text (org-element-property :value target))))
1351 ;;;; Timestamp
1353 (defun org-texinfo-timestamp (timestamp contents info)
1354 "Transcode a TIMESTAMP object from Org to Texinfo.
1355 CONTENTS is nil. INFO is a plist holding contextual
1356 information."
1357 (let ((value (org-texinfo-plain-text
1358 (org-timestamp-translate timestamp) info)))
1359 (case (org-element-property :type timestamp)
1360 ((active active-range)
1361 (format (plist-get info :texinfo-active-timestamp-format) value))
1362 ((inactive inactive-range)
1363 (format (plist-get info :texinfo-inactive-timestamp-format) value))
1364 (t (format (plist-get info :texinfo-diary-timestamp-format) value)))))
1366 ;;;; Verbatim
1368 (defun org-texinfo-verbatim (verbatim contents info)
1369 "Transcode a VERBATIM object from Org to Texinfo.
1370 CONTENTS is nil. INFO is a plist used as a communication
1371 channel."
1372 (org-texinfo--text-markup
1373 (org-element-property :value verbatim) 'verbatim info))
1375 ;;;; Verse Block
1377 (defun org-texinfo-verse-block (verse-block contents info)
1378 "Transcode a VERSE-BLOCK element from Org to Texinfo.
1379 CONTENTS is verse block contents. INFO is a plist holding
1380 contextual information."
1381 (format "@display\n%s@end display" contents))
1384 ;;; Interactive functions
1386 (defun org-texinfo-export-to-texinfo
1387 (&optional async subtreep visible-only body-only ext-plist)
1388 "Export current buffer to a Texinfo file.
1390 If narrowing is active in the current buffer, only export its
1391 narrowed part.
1393 If a region is active, export that region.
1395 A non-nil optional argument ASYNC means the process should happen
1396 asynchronously. The resulting file should be accessible through
1397 the `org-export-stack' interface.
1399 When optional argument SUBTREEP is non-nil, export the sub-tree
1400 at point, extracting information from the headline properties
1401 first.
1403 When optional argument VISIBLE-ONLY is non-nil, don't export
1404 contents of hidden elements.
1406 When optional argument BODY-ONLY is non-nil, only write code
1407 between \"\\begin{document}\" and \"\\end{document}\".
1409 EXT-PLIST, when provided, is a property list with external
1410 parameters overriding Org default settings, but still inferior to
1411 file-local settings.
1413 Return output file's name."
1414 (interactive)
1415 (let ((outfile (org-export-output-file-name ".texi" subtreep))
1416 (org-export-coding-system org-texinfo-coding-system))
1417 (org-export-to-file 'texinfo outfile
1418 async subtreep visible-only body-only ext-plist)))
1420 (defun org-texinfo-export-to-info
1421 (&optional async subtreep visible-only body-only ext-plist)
1422 "Export current buffer to Texinfo then process through to INFO.
1424 If narrowing is active in the current buffer, only export its
1425 narrowed part.
1427 If a region is active, export that region.
1429 A non-nil optional argument ASYNC means the process should happen
1430 asynchronously. The resulting file should be accessible through
1431 the `org-export-stack' interface.
1433 When optional argument SUBTREEP is non-nil, export the sub-tree
1434 at point, extracting information from the headline properties
1435 first.
1437 When optional argument VISIBLE-ONLY is non-nil, don't export
1438 contents of hidden elements.
1440 When optional argument BODY-ONLY is non-nil, only write code
1441 between \"\\begin{document}\" and \"\\end{document}\".
1443 EXT-PLIST, when provided, is a property list with external
1444 parameters overriding Org default settings, but still inferior to
1445 file-local settings.
1447 When optional argument PUB-DIR is set, use it as the publishing
1448 directory.
1450 Return INFO file's name."
1451 (interactive)
1452 (let ((outfile (org-export-output-file-name ".texi" subtreep))
1453 (org-export-coding-system org-texinfo-coding-system))
1454 (org-export-to-file 'texinfo outfile
1455 async subtreep visible-only body-only ext-plist
1456 (lambda (file) (org-texinfo-compile file)))))
1458 ;;;###autoload
1459 (defun org-texinfo-publish-to-texinfo (plist filename pub-dir)
1460 "Publish an org file to Texinfo.
1462 FILENAME is the filename of the Org file to be published. PLIST
1463 is the property list for the given project. PUB-DIR is the
1464 publishing directory.
1466 Return output file name."
1467 (org-publish-org-to 'texinfo filename ".texi" plist pub-dir))
1469 ;;;###autoload
1470 (defun org-texinfo-convert-region-to-texinfo ()
1471 "Assume the current region has org-mode syntax, and convert it to Texinfo.
1472 This can be used in any buffer. For example, you can write an
1473 itemized list in org-mode syntax in an Texinfo buffer and use
1474 this command to convert it."
1475 (interactive)
1476 (org-export-replace-region-by 'texinfo))
1478 (defun org-texinfo-compile (file)
1479 "Compile a texinfo file.
1481 FILE is the name of the file being compiled. Processing is
1482 done through the command specified in `org-texinfo-info-process'.
1484 Return INFO file name or an error if it couldn't be produced."
1485 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1486 (full-name (file-truename file))
1487 (out-dir (file-name-directory file))
1488 ;; Properly set working directory for compilation.
1489 (default-directory (if (file-name-absolute-p file)
1490 (file-name-directory full-name)
1491 default-directory))
1492 errors)
1493 (message (format "Processing Texinfo file %s..." file))
1494 (save-window-excursion
1495 ;; Replace %b, %f and %o with appropriate values in each command
1496 ;; before applying it. Output is redirected to "*Org INFO
1497 ;; Texinfo Output*" buffer.
1498 (let ((outbuf (get-buffer-create "*Org INFO Texinfo Output*")))
1499 (dolist (command org-texinfo-info-process)
1500 (shell-command
1501 (replace-regexp-in-string
1502 "%b" (shell-quote-argument base-name)
1503 (replace-regexp-in-string
1504 "%f" (shell-quote-argument full-name)
1505 (replace-regexp-in-string
1506 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1507 outbuf))
1508 ;; Collect standard errors from output buffer.
1509 (setq errors (org-texinfo-collect-errors outbuf)))
1510 (let ((infofile (concat out-dir base-name ".info")))
1511 ;; Check for process failure. Provide collected errors if
1512 ;; possible.
1513 (if (not (file-exists-p infofile))
1514 (error (concat (format "INFO file %s wasn't produced" infofile)
1515 (when errors (concat ": " errors))))
1516 ;; Else remove log files, when specified, and signal end of
1517 ;; process to user, along with any error encountered.
1518 (when org-texinfo-remove-logfiles
1519 (dolist (ext org-texinfo-logfiles-extensions)
1520 (let ((file (concat out-dir base-name "." ext)))
1521 (when (file-exists-p file) (delete-file file)))))
1522 (message (concat "Process completed"
1523 (if (not errors) "."
1524 (concat " with errors: " errors)))))
1525 ;; Return output file name.
1526 infofile))))
1528 (defun org-texinfo-collect-errors (buffer)
1529 "Collect some kind of errors from \"makeinfo\" command output.
1531 BUFFER is the buffer containing output.
1533 Return collected error types as a string, or nil if there was
1534 none."
1535 (with-current-buffer buffer
1536 (save-excursion
1537 (goto-char (point-min))
1538 ;; Find final "makeinfo" run.
1539 (when t
1540 (let ((case-fold-search t)
1541 (errors ""))
1542 (when (save-excursion
1543 (re-search-forward "perhaps incorrect sectioning?" nil t))
1544 (setq errors (concat errors " [incorrect sectioning]")))
1545 (when (save-excursion
1546 (re-search-forward "missing close brace" nil t))
1547 (setq errors (concat errors " [syntax error]")))
1548 (when (save-excursion
1549 (re-search-forward "Unknown command" nil t))
1550 (setq errors (concat errors " [undefined @command]")))
1551 (when (save-excursion
1552 (re-search-forward "No matching @end" nil t))
1553 (setq errors (concat errors " [block incomplete]")))
1554 (when (save-excursion
1555 (re-search-forward "requires a sectioning" nil t))
1556 (setq errors (concat errors " [invalid section command]")))
1557 (when (save-excursion
1558 (re-search-forward "\\[unexpected\]" nil t))
1559 (setq errors (concat errors " [unexpected error]")))
1560 (when (save-excursion
1561 (re-search-forward "misplaced " nil t))
1562 (setq errors (concat errors " [syntax error]")))
1563 (and (org-string-nw-p errors) (org-trim errors)))))))
1566 (provide 'ox-texinfo)
1568 ;; Local variables:
1569 ;; generated-autoload-file: "org-loaddefs.el"
1570 ;; End:
1572 ;;; ox-texinfo.el ends here