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