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