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