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