Merge branch 'maint'
[org-mode.git] / lisp / ox-texinfo.el
blob18279fab378ef8fab4966c101f0e694ee731c097
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 end of each section.
403 Return new tree."
404 (org-element-map tree 'headline
405 (lambda (hl)
406 (org-element-put-property hl :post-blank 1)
407 (let ((contents (org-element-contents hl)))
408 (when contents
409 (let ((first (org-element-map contents '(headline section)
410 #'identity info t)))
411 (unless (eq (org-element-type first) 'section)
412 (apply #'org-element-set-contents
414 (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 (contents (if (org-string-nw-p contents) (concat "\n" contents) "")))
768 (cond
769 ;; Case 1: This is a footnote section: ignore it.
770 ((org-element-property :footnote-section-p headline) nil)
771 ;; Case 2: This is the `copying' section: ignore it
772 ;; This is used elsewhere.
773 ((org-not-nil (org-element-property :COPYING headline)) nil)
774 ;; Case 3: An index. If it matches one of the known indexes,
775 ;; print it as such following the contents, otherwise
776 ;; print the contents and leave the index up to the user.
777 (index
778 (concat node
779 (format
780 section-fmt
781 full-text
782 (concat contents
783 (and (member index '("cp" "fn" "ky" "pg" "tp" "vr"))
784 (concat "\n@printindex " index))))))
785 ;; Case 4: This is a deep sub-tree: export it as a list item.
786 ;; Also export as items headlines for which no section
787 ;; format has been found.
788 ((or (not section-fmt) (org-export-low-level-p headline info))
789 ;; Build the real contents of the sub-tree.
790 (concat (and (org-export-first-sibling-p headline info)
791 (format "@%s\n" (if numberedp 'enumerate 'itemize)))
792 "@item\n" full-text "\n"
793 contents
794 (if (org-export-last-sibling-p headline info)
795 (format "@end %s" (if numberedp 'enumerate 'itemize))
796 "\n")))
797 ;; Case 5: Standard headline. Export it as a section.
798 (t (concat node (format section-fmt full-text contents))))))
800 (defun org-texinfo-format-headline-default-function
801 (todo todo-type priority text tags)
802 "Default format function for a headline.
803 See `org-texinfo-format-headline-function' for details."
804 (concat (when todo (format "@strong{%s} " todo))
805 (when priority (format "@emph{#%s} " priority))
806 text
807 (when tags (format " :%s:" (mapconcat 'identity tags ":")))))
809 ;;;; Inline Src Block
811 (defun org-texinfo-inline-src-block (inline-src-block contents info)
812 "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
813 CONTENTS holds the contents of the item. INFO is a plist holding
814 contextual information."
815 (let* ((code (org-element-property :value inline-src-block))
816 (separator (org-texinfo--find-verb-separator code)))
817 (concat "@verb{" separator code separator "}")))
819 ;;;; Inlinetask
821 (defun org-texinfo-inlinetask (inlinetask contents info)
822 "Transcode an INLINETASK element from Org to Texinfo.
823 CONTENTS holds the contents of the block. INFO is a plist
824 holding contextual information."
825 (let ((title (org-export-data (org-element-property :title inlinetask) info))
826 (todo (and (plist-get info :with-todo-keywords)
827 (let ((todo (org-element-property :todo-keyword inlinetask)))
828 (and todo (org-export-data todo info)))))
829 (todo-type (org-element-property :todo-type inlinetask))
830 (tags (and (plist-get info :with-tags)
831 (org-export-get-tags inlinetask info)))
832 (priority (and (plist-get info :with-priority)
833 (org-element-property :priority inlinetask))))
834 (funcall (plist-get info :texinfo-format-inlinetask-function)
835 todo todo-type priority title tags contents)))
837 (defun org-texinfo-format-inlinetask-default-function
838 (todo todo-type priority title tags contents)
839 "Default format function for a inlinetasks.
840 See `org-texinfo-format-inlinetask-function' for details."
841 (let ((full-title
842 (concat (when todo (format "@strong{%s} " todo))
843 (when priority (format "#%c " priority))
844 title
845 (when tags (format ":%s:" (mapconcat #'identity tags ":"))))))
846 (format "@center %s\n\n%s\n" full-title contents)))
848 ;;;; Italic
850 (defun org-texinfo-italic (italic contents info)
851 "Transcode ITALIC from Org to Texinfo.
852 CONTENTS is the text with italic markup. INFO is a plist holding
853 contextual information."
854 (org-texinfo--text-markup contents 'italic info))
856 ;;;; Item
858 (defun org-texinfo-item (item contents info)
859 "Transcode an ITEM element from Org to Texinfo.
860 CONTENTS holds the contents of the item. INFO is a plist holding
861 contextual information."
862 (format "@item%s\n%s"
863 (let ((tag (org-element-property :tag item)))
864 (if tag (concat " " (org-export-data tag info)) ""))
865 (or contents "")))
867 ;;;; Keyword
869 (defun org-texinfo-keyword (keyword contents info)
870 "Transcode a KEYWORD element from Org to Texinfo.
871 CONTENTS is nil. INFO is a plist holding contextual information."
872 (let ((key (org-element-property :key keyword))
873 (value (org-element-property :value keyword)))
874 (cond
875 ((string= key "TEXINFO") value)
876 ((string= key "CINDEX") (format "@cindex %s" value))
877 ((string= key "FINDEX") (format "@findex %s" value))
878 ((string= key "KINDEX") (format "@kindex %s" value))
879 ((string= key "PINDEX") (format "@pindex %s" value))
880 ((string= key "TINDEX") (format "@tindex %s" value))
881 ((string= key "VINDEX") (format "@vindex %s" value)))))
883 ;;;; Line Break
885 (defun org-texinfo-line-break (line-break contents info)
886 "Transcode a LINE-BREAK object from Org to Texinfo.
887 CONTENTS is nil. INFO is a plist holding contextual information."
888 "@*\n")
890 ;;;; Link
892 (defun org-texinfo-link (link desc info)
893 "Transcode a LINK object from Org to Texinfo.
895 DESC is the description part of the link, or the empty string.
896 INFO is a plist holding contextual information. See
897 `org-export-data'."
898 (let* ((type (org-element-property :type link))
899 (raw-path (org-element-property :path link))
900 ;; Ensure DESC really exists, or set it to nil.
901 (desc (and (not (string= desc "")) desc))
902 (path (cond
903 ((member type '("http" "https" "ftp"))
904 (concat type ":" raw-path))
905 ((and (string= type "file") (file-name-absolute-p raw-path))
906 (concat "file:" raw-path))
907 (t raw-path)))
908 (email (if (string= type "mailto")
909 (let ((text (replace-regexp-in-string
910 "@" "@@" raw-path)))
911 (concat text (if desc (concat "," desc))))))
912 protocol)
913 (cond
914 ;; Links pointing to a headline: Find destination and build
915 ;; appropriate referencing command.
916 ((member type '("custom-id" "id"))
917 (let ((destination (org-export-resolve-id-link link info)))
918 (case (org-element-type destination)
919 ;; Id link points to an external file.
920 (plain-text
921 (if desc (format "@uref{file://%s,%s}" destination desc)
922 (format "@uref{file://%s}" destination)))
923 ;; LINK points to a headline. Use the headline as the NODE target
924 (headline
925 (format "@ref{%s,%s}"
926 (org-texinfo--get-node destination info)
927 (or desc "")))
928 (otherwise
929 (let ((path (org-export-solidify-link-text path)))
930 (if (not desc) (format "@ref{%s}" path)
931 (format "@ref{%s,,%s}" path desc)))))))
932 ((member type '("info"))
933 (let* ((info-path (split-string path "[:#]"))
934 (info-manual (car info-path))
935 (info-node (or (cadr info-path) "top"))
936 (title (or desc "")))
937 (format "@ref{%s,%s,,%s,}" info-node title info-manual)))
938 ((member type '("fuzzy"))
939 (let ((destination (org-export-resolve-fuzzy-link link info)))
940 (case (org-element-type destination)
941 ;; Id link points to an external file.
942 (plain-text
943 (if desc (format "@uref{file://%s,%s}" destination desc)
944 (format "@uref{file://%s}" destination)))
945 ;; LINK points to a headline. Use the headline as the NODE target
946 (headline
947 (format "@ref{%s,%s}"
948 (org-texinfo--get-node destination info)
949 (or desc "")))
950 (otherwise
951 (let ((path (org-export-solidify-link-text path)))
952 (if (not desc) (format "@ref{%s}" path)
953 (format "@ref{%s,,%s}" path desc)))))))
954 ;; Special case for email addresses
955 (email
956 (format "@email{%s}" email))
957 ;; External link with a description part.
958 ((and path desc) (format "@uref{%s,%s}" path desc))
959 ;; External link without a description part.
960 (path (format "@uref{%s}" path))
961 ;; No path, only description. Try to do something useful.
963 (format (plist-get info :texinfo-link-with-unknown-path-format) desc)))))
966 ;;;; Menu
968 (defun org-texinfo-make-menu (scope info &optional master)
969 "Create the menu for inclusion in the Texinfo document.
971 SCOPE is a headline or a full parse tree. INFO is the
972 communication channel, as a plist.
974 When optional argument MASTER is non-nil, generate a master menu,
975 including detailed node listing."
976 (let ((menu (org-texinfo--build-menu scope info)))
977 (when (org-string-nw-p menu)
978 (org-element-normalize-string
979 (format
980 "@menu\n%s@end menu"
981 (concat menu
982 (when master
983 (let ((detailmenu
984 (org-texinfo--build-menu
985 scope info
986 (let ((toc-depth (plist-get info :with-toc)))
987 (if (wholenump toc-depth) toc-depth
988 org-texinfo-max-toc-depth)))))
989 (when (org-string-nw-p detailmenu)
990 (concat "\n@detailmenu\n"
991 "--- The Detailed Node Listing ---\n\n"
992 detailmenu
993 "@end detailmenu\n"))))))))))
995 (defun org-texinfo--build-menu (scope info &optional level)
996 "Build menu for entries within SCOPE.
997 SCOPE is a headline or a full parse tree. INFO is a plist
998 containing contextual information. When optional argument LEVEL
999 is an integer, build the menu recursively, down to this depth."
1000 (cond
1001 ((not level)
1002 (org-texinfo--format-entries (org-texinfo--menu-entries scope info) info))
1003 ((zerop level) nil)
1005 (org-element-normalize-string
1006 (mapconcat
1007 (lambda (h)
1008 (let ((entries (org-texinfo--menu-entries h info)))
1009 (when entries
1010 (concat
1011 (format "%s\n\n%s\n"
1012 (org-export-data (org-export-get-alt-title h info) info)
1013 (org-texinfo--format-entries entries info))
1014 (org-texinfo--build-menu h info (1- level))))))
1015 (org-texinfo--menu-entries scope info) "")))))
1017 (defun org-texinfo--format-entries (entries info)
1018 "Format all direct menu entries in SCOPE, as a string.
1019 SCOPE is either a headline or a full Org document. INFO is
1020 a plist containing contextual information."
1021 (org-element-normalize-string
1022 (mapconcat
1023 (lambda (h)
1024 (let* ((title (org-export-data
1025 (org-export-get-alt-title h info) info))
1026 (node (org-texinfo--get-node h info))
1027 (entry (concat "* " title ":"
1028 (if (string= title node) ":"
1029 (concat " " node ". "))))
1030 (desc (org-element-property :DESCRIPTION h)))
1031 (if (not desc) entry
1032 (format (format "%%-%ds %%s" org-texinfo-node-description-column)
1033 entry desc))))
1034 entries "\n")))
1036 (defun org-texinfo--menu-entries (scope info)
1037 "List direct children in SCOPE needing a menu entry.
1038 SCOPE is a headline or a full parse tree. INFO is a plist
1039 holding contextual information."
1040 (let* ((cache (or (plist-get info :texinfo-entries-cache)
1041 (plist-get (plist-put info :texinfo-entries-cache
1042 (make-hash-table :test #'eq))
1043 :texinfo-entries-cache)))
1044 (cached-entries (gethash scope cache 'no-cache)))
1045 (if (not (eq cached-entries 'no-cache)) cached-entries
1046 (puthash scope
1047 (org-element-map (org-element-contents scope) 'headline
1048 (lambda (h)
1049 (and (not (org-not-nil (org-element-property :COPYING h)))
1050 (not (org-element-property :footnote-section-p h))
1051 (not (org-export-low-level-p h info))
1053 info nil 'headline)
1054 cache))))
1056 ;;;; Node Property
1058 (defun org-texinfo-node-property (node-property contents info)
1059 "Transcode a NODE-PROPERTY element from Org to Texinfo.
1060 CONTENTS is nil. INFO is a plist holding contextual
1061 information."
1062 (format "%s:%s"
1063 (org-element-property :key node-property)
1064 (let ((value (org-element-property :value node-property)))
1065 (if value (concat " " value) ""))))
1067 ;;;; Paragraph
1069 (defun org-texinfo-paragraph (paragraph contents info)
1070 "Transcode a PARAGRAPH element from Org to Texinfo.
1071 CONTENTS is the contents of the paragraph, as a string. INFO is
1072 the plist used as a communication channel."
1073 contents)
1075 ;;;; Plain List
1077 (defun org-texinfo-plain-list (plain-list contents info)
1078 "Transcode a PLAIN-LIST element from Org to Texinfo.
1079 CONTENTS is the contents of the list. INFO is a plist holding
1080 contextual information."
1081 (let* ((attr (org-export-read-attribute :attr_texinfo plain-list))
1082 (indic (or (plist-get attr :indic)
1083 (plist-get info :texinfo-def-table-markup)))
1084 (table-type (plist-get attr :table-type))
1085 (type (org-element-property :type plain-list))
1086 (list-type (cond
1087 ((eq type 'ordered) "enumerate")
1088 ((eq type 'unordered) "itemize")
1089 ((member table-type '("ftable" "vtable")) table-type)
1090 (t "table"))))
1091 (format "@%s\n%s@end %s"
1092 (if (eq type 'descriptive) (concat list-type " " indic) list-type)
1093 contents
1094 list-type)))
1096 ;;;; Plain Text
1098 (defun org-texinfo-plain-text (text info)
1099 "Transcode a TEXT string from Org to Texinfo.
1100 TEXT is the string to transcode. INFO is a plist holding
1101 contextual information."
1102 ;; First protect @, { and }.
1103 (let ((output (org-texinfo--sanitize-content text)))
1104 ;; Activate smart quotes. Be sure to provide original TEXT string
1105 ;; since OUTPUT may have been modified.
1106 (when (plist-get info :with-smart-quotes)
1107 (setq output
1108 (org-export-activate-smart-quotes output :texinfo info text)))
1109 ;; LaTeX into @LaTeX{} and TeX into @TeX{}
1110 (let ((case-fold-search nil)
1111 (start 0))
1112 (while (string-match "\\(\\(?:La\\)?TeX\\)" output start)
1113 (setq output (replace-match
1114 (format "@%s{}" (match-string 1 output)) nil t output)
1115 start (match-end 0))))
1116 ;; Convert special strings.
1117 (when (plist-get info :with-special-strings)
1118 (while (string-match (regexp-quote "...") output)
1119 (setq output (replace-match "@dots{}" nil t output))))
1120 ;; Handle break preservation if required.
1121 (when (plist-get info :preserve-breaks)
1122 (setq output (replace-regexp-in-string
1123 "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" output)))
1124 ;; Return value.
1125 output))
1127 ;;;; Planning
1129 (defun org-texinfo-planning (planning contents info)
1130 "Transcode a PLANNING element from Org to Texinfo.
1131 CONTENTS is nil. INFO is a plist holding contextual
1132 information."
1133 (concat
1134 "@noindent"
1135 (mapconcat
1136 'identity
1137 (delq nil
1138 (list
1139 (let ((closed (org-element-property :closed planning)))
1140 (when closed
1141 (concat
1142 (format "@strong{%s} " org-closed-string)
1143 (format (plist-get info :texinfo-inactive-timestamp-format)
1144 (org-translate-time
1145 (org-element-property :raw-value closed))))))
1146 (let ((deadline (org-element-property :deadline planning)))
1147 (when deadline
1148 (concat
1149 (format "@strong{%s} " org-deadline-string)
1150 (format (plist-get info :texinfo-active-timestamp-format)
1151 (org-translate-time
1152 (org-element-property :raw-value deadline))))))
1153 (let ((scheduled (org-element-property :scheduled planning)))
1154 (when scheduled
1155 (concat
1156 (format "@strong{%s} " org-scheduled-string)
1157 (format (plist-get info :texinfo-active-timestamp-format)
1158 (org-translate-time
1159 (org-element-property :raw-value scheduled))))))))
1160 " ")
1161 "@*"))
1163 ;;;; Property Drawer
1165 (defun org-texinfo-property-drawer (property-drawer contents info)
1166 "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
1167 CONTENTS holds the contents of the drawer. INFO is a plist
1168 holding contextual information."
1169 (and (org-string-nw-p contents)
1170 (format "@verbatim\n%s@end verbatim" contents)))
1172 ;;;; Quote Block
1174 (defun org-texinfo-quote-block (quote-block contents info)
1175 "Transcode a QUOTE-BLOCK element from Org to Texinfo.
1176 CONTENTS holds the contents of the block. INFO is a plist
1177 holding contextual information."
1178 (let* ((title (org-element-property :name quote-block))
1179 (start-quote (concat "@quotation"
1180 (if title
1181 (format " %s" title)))))
1182 (format "%s\n%s@end quotation" start-quote contents)))
1184 ;;;; Radio Target
1186 (defun org-texinfo-radio-target (radio-target text info)
1187 "Transcode a RADIO-TARGET object from Org to Texinfo.
1188 TEXT is the text of the target. INFO is a plist holding
1189 contextual information."
1190 (format "@anchor{%s}%s"
1191 (org-export-solidify-link-text
1192 (org-element-property :value radio-target))
1193 text))
1195 ;;;; Section
1197 (defun org-texinfo-section (section contents info)
1198 "Transcode a SECTION element from Org to Texinfo.
1199 CONTENTS holds the contents of the section. INFO is a plist
1200 holding contextual information."
1201 (concat contents
1202 (let ((parent (org-export-get-parent-headline section)))
1203 (and parent (org-texinfo-make-menu parent info)))))
1205 ;;;; Special Block
1207 (defun org-texinfo-special-block (special-block contents info)
1208 "Transcode a SPECIAL-BLOCK element from Org to Texinfo.
1209 CONTENTS holds the contents of the block. INFO is a plist used
1210 as a communication channel."
1211 (if (org-export-raw-special-block-p special-block info)
1212 (org-remove-indentation (org-element-property :raw-value special-block))
1213 contents))
1215 ;;;; Src Block
1217 (defun org-texinfo-src-block (src-block contents info)
1218 "Transcode a SRC-BLOCK element from Org to Texinfo.
1219 CONTENTS holds the contents of the item. INFO is a plist holding
1220 contextual information."
1221 (let ((lispp (org-string-match-p "lisp"
1222 (org-element-property :language src-block)))
1223 (code (org-texinfo--sanitize-content
1224 (org-export-format-code-default src-block info))))
1225 (format (if lispp "@lisp\n%s@end lisp" "@example\n%s@end example") code)))
1227 ;;;; Statistics Cookie
1229 (defun org-texinfo-statistics-cookie (statistics-cookie contents info)
1230 "Transcode a STATISTICS-COOKIE object from Org to Texinfo.
1231 CONTENTS is nil. INFO is a plist holding contextual information."
1232 (org-element-property :value statistics-cookie))
1234 ;;;; Subscript
1236 (defun org-texinfo-subscript (subscript contents info)
1237 "Transcode a SUBSCRIPT object from Org to Texinfo.
1238 CONTENTS is the contents of the object. INFO is a plist holding
1239 contextual information."
1240 (format "@math{_%s}" contents))
1242 ;;;; Superscript
1244 (defun org-texinfo-superscript (superscript contents info)
1245 "Transcode a SUPERSCRIPT object from Org to Texinfo.
1246 CONTENTS is the contents of the object. INFO is a plist holding
1247 contextual information."
1248 (format "@math{^%s}" contents))
1250 ;;;; Table
1252 (defun org-texinfo-table (table contents info)
1253 "Transcode a TABLE element from Org to Texinfo.
1254 CONTENTS is the contents of the table. INFO is a plist holding
1255 contextual information."
1256 (if (eq (org-element-property :type table) 'table.el)
1257 (format "@verbatim\n%s@end verbatim"
1258 (org-element-normalize-string
1259 (org-element-property :value table)))
1260 (let* ((col-width (org-export-read-attribute :attr_texinfo table :columns))
1261 (columns
1262 (if col-width (format "@columnfractions %s" col-width)
1263 (org-texinfo-table-column-widths table info))))
1264 (format "@multitable %s\n%s@end multitable"
1265 columns
1266 contents))))
1268 (defun org-texinfo-table-column-widths (table info)
1269 "Determine the largest table cell in each column to process alignment.
1270 TABLE is the table element to transcode. INFO is a plist used as
1271 a communication channel."
1272 (let ((widths (make-vector (cdr (org-export-table-dimensions table info)) 0)))
1273 (org-element-map table 'table-row
1274 (lambda (row)
1275 (let ((idx 0))
1276 (org-element-map row 'table-cell
1277 (lambda (cell)
1278 ;; Length of the cell in the original buffer is only an
1279 ;; approximation of the length of the cell in the
1280 ;; output. It can sometimes fail (e.g. it considers
1281 ;; "/a/" being larger than "ab").
1282 (let ((w (- (org-element-property :contents-end cell)
1283 (org-element-property :contents-begin cell))))
1284 (aset widths idx (max w (aref widths idx))))
1285 (incf idx))
1286 info)))
1287 info)
1288 (format "{%s}" (mapconcat (lambda (w) (make-string w ?a)) widths "} {"))))
1290 ;;;; Table Cell
1292 (defun org-texinfo-table-cell (table-cell contents info)
1293 "Transcode a TABLE-CELL element from Org to Texinfo.
1294 CONTENTS is the cell contents. INFO is a plist used as
1295 a communication channel."
1296 (concat
1297 (let ((scientific-notation
1298 (plist-get info :texinfo-table-scientific-notation)))
1299 (if (and contents
1300 scientific-notation
1301 (string-match orgtbl-exp-regexp contents))
1302 ;; Use appropriate format string for scientific notation.
1303 (format scientific-notation
1304 (match-string 1 contents)
1305 (match-string 2 contents))
1306 contents))
1307 (when (org-export-get-next-element table-cell info) "\n@tab ")))
1309 ;;;; Table Row
1311 (defun org-texinfo-table-row (table-row contents info)
1312 "Transcode a TABLE-ROW element from Org to Texinfo.
1313 CONTENTS is the contents of the row. INFO is a plist used as
1314 a communication channel."
1315 ;; Rules are ignored since table separators are deduced from
1316 ;; borders of the current row.
1317 (when (eq (org-element-property :type table-row) 'standard)
1318 (let ((rowgroup-tag
1319 (if (and (= 1 (org-export-table-row-group table-row info))
1320 (org-export-table-has-header-p
1321 (org-export-get-parent-table table-row) info))
1322 "@headitem "
1323 "@item ")))
1324 (concat rowgroup-tag contents "\n"))))
1326 ;;;; Target
1328 (defun org-texinfo-target (target contents info)
1329 "Transcode a TARGET object from Org to Texinfo.
1330 CONTENTS is nil. INFO is a plist holding contextual
1331 information."
1332 (format "@anchor{%s}"
1333 (org-export-solidify-link-text (org-element-property :value target))))
1335 ;;;; Timestamp
1337 (defun org-texinfo-timestamp (timestamp contents info)
1338 "Transcode a TIMESTAMP object from Org to Texinfo.
1339 CONTENTS is nil. INFO is a plist holding contextual
1340 information."
1341 (let ((value (org-texinfo-plain-text
1342 (org-timestamp-translate timestamp) info)))
1343 (case (org-element-property :type timestamp)
1344 ((active active-range)
1345 (format (plist-get info :texinfo-active-timestamp-format) value))
1346 ((inactive inactive-range)
1347 (format (plist-get info :texinfo-inactive-timestamp-format) value))
1348 (t (format (plist-get info :texinfo-diary-timestamp-format) value)))))
1350 ;;;; Verbatim
1352 (defun org-texinfo-verbatim (verbatim contents info)
1353 "Transcode a VERBATIM object from Org to Texinfo.
1354 CONTENTS is nil. INFO is a plist used as a communication
1355 channel."
1356 (org-texinfo--text-markup
1357 (org-element-property :value verbatim) 'verbatim info))
1359 ;;;; Verse Block
1361 (defun org-texinfo-verse-block (verse-block contents info)
1362 "Transcode a VERSE-BLOCK element from Org to Texinfo.
1363 CONTENTS is verse block contents. INFO is a plist holding
1364 contextual information."
1365 (format "@display\n%s@end display" contents))
1368 ;;; Interactive functions
1370 (defun org-texinfo-export-to-texinfo
1371 (&optional async subtreep visible-only body-only ext-plist)
1372 "Export current buffer to a Texinfo file.
1374 If narrowing is active in the current buffer, only export its
1375 narrowed part.
1377 If a region is active, export that region.
1379 A non-nil optional argument ASYNC means the process should happen
1380 asynchronously. The resulting file should be accessible through
1381 the `org-export-stack' interface.
1383 When optional argument SUBTREEP is non-nil, export the sub-tree
1384 at point, extracting information from the headline properties
1385 first.
1387 When optional argument VISIBLE-ONLY is non-nil, don't export
1388 contents of hidden elements.
1390 When optional argument BODY-ONLY is non-nil, only write code
1391 between \"\\begin{document}\" and \"\\end{document}\".
1393 EXT-PLIST, when provided, is a property list with external
1394 parameters overriding Org default settings, but still inferior to
1395 file-local settings.
1397 Return output file's name."
1398 (interactive)
1399 (let ((outfile (org-export-output-file-name ".texi" subtreep))
1400 (org-export-coding-system org-texinfo-coding-system))
1401 (org-export-to-file 'texinfo outfile
1402 async subtreep visible-only body-only ext-plist)))
1404 (defun org-texinfo-export-to-info
1405 (&optional async subtreep visible-only body-only ext-plist)
1406 "Export current buffer to Texinfo then process through to INFO.
1408 If narrowing is active in the current buffer, only export its
1409 narrowed part.
1411 If a region is active, export that region.
1413 A non-nil optional argument ASYNC means the process should happen
1414 asynchronously. The resulting file should be accessible through
1415 the `org-export-stack' interface.
1417 When optional argument SUBTREEP is non-nil, export the sub-tree
1418 at point, extracting information from the headline properties
1419 first.
1421 When optional argument VISIBLE-ONLY is non-nil, don't export
1422 contents of hidden elements.
1424 When optional argument BODY-ONLY is non-nil, only write code
1425 between \"\\begin{document}\" and \"\\end{document}\".
1427 EXT-PLIST, when provided, is a property list with external
1428 parameters overriding Org default settings, but still inferior to
1429 file-local settings.
1431 When optional argument PUB-DIR is set, use it as the publishing
1432 directory.
1434 Return INFO file's name."
1435 (interactive)
1436 (let ((outfile (org-export-output-file-name ".texi" subtreep))
1437 (org-export-coding-system org-texinfo-coding-system))
1438 (org-export-to-file 'texinfo outfile
1439 async subtreep visible-only body-only ext-plist
1440 (lambda (file) (org-texinfo-compile file)))))
1442 ;;;###autoload
1443 (defun org-texinfo-publish-to-texinfo (plist filename pub-dir)
1444 "Publish an org file to Texinfo.
1446 FILENAME is the filename of the Org file to be published. PLIST
1447 is the property list for the given project. PUB-DIR is the
1448 publishing directory.
1450 Return output file name."
1451 (org-publish-org-to 'texinfo filename ".texi" plist pub-dir))
1453 ;;;###autoload
1454 (defun org-texinfo-convert-region-to-texinfo ()
1455 "Assume the current region has org-mode syntax, and convert it to Texinfo.
1456 This can be used in any buffer. For example, you can write an
1457 itemized list in org-mode syntax in an Texinfo buffer and use
1458 this command to convert it."
1459 (interactive)
1460 (org-export-replace-region-by 'texinfo))
1462 (defun org-texinfo-compile (file)
1463 "Compile a texinfo file.
1465 FILE is the name of the file being compiled. Processing is
1466 done through the command specified in `org-texinfo-info-process'.
1468 Return INFO file name or an error if it couldn't be produced."
1469 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1470 (full-name (file-truename file))
1471 (out-dir (file-name-directory file))
1472 ;; Properly set working directory for compilation.
1473 (default-directory (if (file-name-absolute-p file)
1474 (file-name-directory full-name)
1475 default-directory))
1476 errors)
1477 (message (format "Processing Texinfo file %s..." file))
1478 (save-window-excursion
1479 ;; Replace %b, %f and %o with appropriate values in each command
1480 ;; before applying it. Output is redirected to "*Org INFO
1481 ;; Texinfo Output*" buffer.
1482 (let ((outbuf (get-buffer-create "*Org INFO Texinfo Output*")))
1483 (dolist (command org-texinfo-info-process)
1484 (shell-command
1485 (replace-regexp-in-string
1486 "%b" (shell-quote-argument base-name)
1487 (replace-regexp-in-string
1488 "%f" (shell-quote-argument full-name)
1489 (replace-regexp-in-string
1490 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1491 outbuf))
1492 ;; Collect standard errors from output buffer.
1493 (setq errors (org-texinfo-collect-errors outbuf)))
1494 (let ((infofile (concat out-dir base-name ".info")))
1495 ;; Check for process failure. Provide collected errors if
1496 ;; possible.
1497 (if (not (file-exists-p infofile))
1498 (error (concat (format "INFO file %s wasn't produced" infofile)
1499 (when errors (concat ": " errors))))
1500 ;; Else remove log files, when specified, and signal end of
1501 ;; process to user, along with any error encountered.
1502 (when org-texinfo-remove-logfiles
1503 (dolist (ext org-texinfo-logfiles-extensions)
1504 (let ((file (concat out-dir base-name "." ext)))
1505 (when (file-exists-p file) (delete-file file)))))
1506 (message (concat "Process completed"
1507 (if (not errors) "."
1508 (concat " with errors: " errors)))))
1509 ;; Return output file name.
1510 infofile))))
1512 (defun org-texinfo-collect-errors (buffer)
1513 "Collect some kind of errors from \"makeinfo\" command output.
1515 BUFFER is the buffer containing output.
1517 Return collected error types as a string, or nil if there was
1518 none."
1519 (with-current-buffer buffer
1520 (save-excursion
1521 (goto-char (point-min))
1522 ;; Find final "makeinfo" run.
1523 (when t
1524 (let ((case-fold-search t)
1525 (errors ""))
1526 (when (save-excursion
1527 (re-search-forward "perhaps incorrect sectioning?" nil t))
1528 (setq errors (concat errors " [incorrect sectioning]")))
1529 (when (save-excursion
1530 (re-search-forward "missing close brace" nil t))
1531 (setq errors (concat errors " [syntax error]")))
1532 (when (save-excursion
1533 (re-search-forward "Unknown command" nil t))
1534 (setq errors (concat errors " [undefined @command]")))
1535 (when (save-excursion
1536 (re-search-forward "No matching @end" nil t))
1537 (setq errors (concat errors " [block incomplete]")))
1538 (when (save-excursion
1539 (re-search-forward "requires a sectioning" nil t))
1540 (setq errors (concat errors " [invalid section command]")))
1541 (when (save-excursion
1542 (re-search-forward "\\[unexpected\]" nil t))
1543 (setq errors (concat errors " [unexpected error]")))
1544 (when (save-excursion
1545 (re-search-forward "misplaced " nil t))
1546 (setq errors (concat errors " [syntax error]")))
1547 (and (org-string-nw-p errors) (org-trim errors)))))))
1550 (provide 'ox-texinfo)
1552 ;; Local variables:
1553 ;; generated-autoload-file: "org-loaddefs.el"
1554 ;; End:
1556 ;;; ox-texinfo.el ends here