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