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