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