org.texi (Publishing action, Complex example): Fix names of publishing functions
[org-mode.git] / lisp / ox-texinfo.el
blob24a52d5cd0960aeee36e5f898038afb8d7c35a60
1 ;;; ox-texinfo.el --- Texinfo Back-End for Org Export Engine
3 ;; Copyright (C) 2012, 2013 Jonathan Leech-Pepin
4 ;; Author: Jonathan Leech-Pepin <jonathan.leechpepin at gmail dot com>
5 ;; Keywords: outlines, hypermedia, calendar, wp
6 ;;
7 ;; This file is not part of GNU Emacs.
8 ;;
9 ;; This program 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 ;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;; This library implements a Texinfo back-end for Org generic
25 ;; exporter.
27 ;; To test it, run
29 ;; M-: (org-export-to-buffer 'texinfo "*Test Texinfo*") RET
31 ;; in an Org mode buffer then switch to the buffer to see the Texinfo
32 ;; export. See ox.el for more details on how this exporter works.
35 ;; It introduces nine new buffer keywords: "TEXINFO_CLASS",
36 ;; "TEXINFO_FILENAME", "TEXINFO_HEADER", "TEXINFO_POST_HEADER",
37 ;; "TEXINFO_DIR_CATEGORY", "TEXINFO_DIR_TITLE", "TEXINFO_DIR_DESC"
38 ;; "SUBTITLE" and "SUBAUTHOR".
41 ;; It introduces 1 new headline property keywords:
42 ;; "TEXINFO_MENU_TITLE" for optional menu titles.
44 ;; To include inline code snippets (for example for generating @kbd{}
45 ;; and @key{} commands), the following export-snippet keys are
46 ;; accepted:
48 ;; texinfo
49 ;; info
51 ;; You can add them for export snippets via any of the below:
53 ;; (add-to-list 'org-export-snippet-translation-alist
54 ;; '("info" . "texinfo"))
57 ;;; Code:
59 (eval-when-compile (require 'cl))
60 (require 'ox)
62 (defvar orgtbl-exp-regexp)
66 ;;; Define Back-End
68 (org-export-define-backend texinfo
69 ((bold . org-texinfo-bold)
70 (center-block . org-texinfo-center-block)
71 (clock . org-texinfo-clock)
72 (code . org-texinfo-code)
73 (comment . org-texinfo-comment)
74 (comment-block . org-texinfo-comment-block)
75 (drawer . org-texinfo-drawer)
76 (dynamic-block . org-texinfo-dynamic-block)
77 (entity . org-texinfo-entity)
78 (example-block . org-texinfo-example-block)
79 (export-block . org-texinfo-export-block)
80 (export-snippet . org-texinfo-export-snippet)
81 (fixed-width . org-texinfo-fixed-width)
82 (footnote-definition . org-texinfo-footnote-definition)
83 (footnote-reference . org-texinfo-footnote-reference)
84 (headline . org-texinfo-headline)
85 (inline-src-block . org-texinfo-inline-src-block)
86 (inlinetask . org-texinfo-inlinetask)
87 (italic . org-texinfo-italic)
88 (item . org-texinfo-item)
89 (keyword . org-texinfo-keyword)
90 (line-break . org-texinfo-line-break)
91 (link . org-texinfo-link)
92 (paragraph . org-texinfo-paragraph)
93 (plain-list . org-texinfo-plain-list)
94 (plain-text . org-texinfo-plain-text)
95 (planning . org-texinfo-planning)
96 (property-drawer . org-texinfo-property-drawer)
97 (quote-block . org-texinfo-quote-block)
98 (quote-section . org-texinfo-quote-section)
99 (radio-target . org-texinfo-radio-target)
100 (section . org-texinfo-section)
101 (special-block . org-texinfo-special-block)
102 (src-block . org-texinfo-src-block)
103 (statistics-cookie . org-texinfo-statistics-cookie)
104 (subscript . org-texinfo-subscript)
105 (superscript . org-texinfo-superscript)
106 (table . org-texinfo-table)
107 (table-cell . org-texinfo-table-cell)
108 (table-row . org-texinfo-table-row)
109 (target . org-texinfo-target)
110 (template . org-texinfo-template)
111 (timestamp . org-texinfo-timestamp)
112 (verbatim . org-texinfo-verbatim)
113 (verse-block . org-texinfo-verse-block))
114 :export-block "TEXINFO"
115 :filters-alist
116 ((:filter-headline . org-texinfo-filter-section-blank-lines)
117 (:filter-section . org-texinfo-filter-section-blank-lines))
118 :menu-entry
119 (?i "Export to Texinfo"
120 ((?t "As TEXI file" org-texinfo-export-to-texinfo)
121 (?i "As INFO file" org-texinfo-export-to-info)))
122 :options-alist
123 ((:texinfo-filename "TEXINFO_FILENAME" nil org-texinfo-filename t)
124 (:texinfo-class "TEXINFO_CLASS" nil org-texinfo-default-class t)
125 (:texinfo-header "TEXINFO_HEADER" nil nil newline)
126 (:texinfo-post-header "TEXINFO_POST_HEADER" nil nil newline)
127 (:subtitle "SUBTITLE" nil nil newline)
128 (:subauthor "SUBAUTHOR" nil nil newline)
129 (:texinfo-dircat "TEXINFO_DIR_CATEGORY" nil nil t)
130 (:texinfo-dirtitle "TEXINFO_DIR_TITLE" nil nil t)
131 (:texinfo-dirdesc "TEXINFO_DIR_DESC" nil nil t)
132 (:texinfo-menu-title "TEXINFO_MENU_TITLE" nil nil newline)))
136 ;;; User Configurable Variables
138 (defgroup org-export-texinfo nil
139 "Options for exporting Org mode files to Texinfo."
140 :tag "Org Export Texinfo"
141 :group 'org-export)
143 ;;; Preamble
145 (defcustom org-texinfo-filename nil
146 "Default filename for texinfo output."
147 :group 'org-export-texinfo
148 :type '(string :tag "Export Filename"))
150 (defcustom org-texinfo-default-class "info"
151 "The default Texinfo class."
152 :group 'org-export-texinfo
153 :type '(string :tag "Texinfo class"))
155 (defcustom org-texinfo-classes
156 '(("info"
157 "\\input texinfo @c -*- texinfo -*-"
158 ("@chapter %s" . "@unnumbered %s")
159 ("@section %s" . "@unnumberedsec %s")
160 ("@subsection %s" . "@unnumberedsubsec %s")
161 ("@subsubsection %s" . "@unnumberedsubsubsec %s")))
162 "Alist of Texinfo classes and associated header and structure.
163 If #+Texinfo_CLASS is set in the buffer, use its value and the
164 associated information. Here is the structure of each cell:
166 \(class-name
167 header-string
168 \(numbered-section . unnumbered-section\)
169 ...\)
171 The sectioning structure
172 ------------------------
174 The sectioning structure of the class is given by the elements
175 following the header string. For each sectioning level, a number
176 of strings is specified. A %s formatter is mandatory in each
177 section string and will be replaced by the title of the section.
179 Instead of a list of sectioning commands, you can also specify
180 a function name. That function will be called with two
181 parameters, the \(reduced) level of the headline, and a predicate
182 non-nil when the headline should be numbered. It must return
183 a format string in which the section title will be added."
184 :group 'org-export-texinfo
185 :type '(repeat
186 (list (string :tag "Texinfo class")
187 (string :tag "Texinfo header")
188 (repeat :tag "Levels" :inline t
189 (choice
190 (cons :tag "Heading"
191 (string :tag " numbered")
192 (string :tag "unnumbered"))
193 (function :tag "Hook computing sectioning"))))))
195 ;;; Headline
197 (defcustom org-texinfo-format-headline-function nil
198 "Function to format headline text.
200 This function will be called with 5 arguments:
201 TODO the todo keyword (string or nil).
202 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
203 PRIORITY the priority of the headline (integer or nil)
204 TEXT the main headline text (string).
205 TAGS the tags as a list of strings (list of strings or nil).
207 The function result will be used in the section format string.
209 As an example, one could set the variable to the following, in
210 order to reproduce the default set-up:
212 \(defun org-texinfo-format-headline (todo todo-type priority text tags)
213 \"Default format function for an headline.\"
214 \(concat (when todo
215 \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo))
216 \(when priority
217 \(format \"\\\\framebox{\\\\#%c} \" priority))
218 text
219 \(when tags
220 \(format \"\\\\hfill{}\\\\textsc{%s}\"
221 \(mapconcat 'identity tags \":\"))))"
222 :group 'org-export-texinfo
223 :type 'function)
226 ;;; Footnotes
228 ;; Footnotes are inserted directly
230 ;;; Timestamps
232 (defcustom org-texinfo-active-timestamp-format "@emph{%s}"
233 "A printf format string to be applied to active timestamps."
234 :group 'org-export-texinfo
235 :type 'string)
237 (defcustom org-texinfo-inactive-timestamp-format "@emph{%s}"
238 "A printf format string to be applied to inactive timestamps."
239 :group 'org-export-texinfo
240 :type 'string)
242 (defcustom org-texinfo-diary-timestamp-format "@emph{%s}"
243 "A printf format string to be applied to diary timestamps."
244 :group 'org-export-texinfo
245 :type 'string)
247 ;;; Links
249 (defcustom org-texinfo-link-with-unknown-path-format "@indicateurl{%s}"
250 "Format string for links with unknown path type."
251 :group 'org-export-texinfo
252 :type 'string)
254 ;;; Tables
256 (defcustom org-texinfo-tables-verbatim nil
257 "When non-nil, tables are exported verbatim."
258 :group 'org-export-texinfo
259 :type 'boolean)
261 (defcustom org-texinfo-table-scientific-notation "%s\\,(%s)"
262 "Format string to display numbers in scientific notation.
263 The format should have \"%s\" twice, for mantissa and exponent
264 \(i.e. \"%s\\\\times10^{%s}\").
266 When nil, no transformation is made."
267 :group 'org-export-texinfo
268 :type '(choice
269 (string :tag "Format string")
270 (const :tag "No formatting")))
272 (defcustom org-texinfo-def-table-markup "@samp"
273 "Default setting for @table environments.")
275 ;;; Text markup
277 (defcustom org-texinfo-text-markup-alist '((bold . "@strong{%s}")
278 (code . code)
279 (italic . "@emph{%s}")
280 (verbatim . verb)
281 (comment . "@c %s"))
282 "Alist of Texinfo expressions to convert text markup.
284 The key must be a symbol among `bold', `italic' and `comment'.
285 The value is a formatting string to wrap fontified text with.
287 Value can also be set to the following symbols: `verb' and
288 `code'. For the former, Org will use \"@verb\" to
289 create a format string and select a delimiter character that
290 isn't in the string. For the latter, Org will use \"@code\"
291 to typeset and try to protect special characters.
293 If no association can be found for a given markup, text will be
294 returned as-is."
295 :group 'org-export-texinfo
296 :type 'alist
297 :options '(bold code italic verbatim comment))
299 ;;; Drawers
301 (defcustom org-texinfo-format-drawer-function nil
302 "Function called to format a drawer in Texinfo code.
304 The function must accept two parameters:
305 NAME the drawer name, like \"LOGBOOK\"
306 CONTENTS the contents of the drawer.
308 The function should return the string to be exported.
310 For example, the variable could be set to the following function
311 in order to mimic default behaviour:
313 \(defun org-texinfo-format-drawer-default \(name contents\)
314 \"Format a drawer element for Texinfo export.\"
315 contents\)"
316 :group 'org-export-texinfo
317 :type 'function)
319 ;;; Inlinetasks
321 (defcustom org-texinfo-format-inlinetask-function nil
322 "Function called to format an inlinetask in Texinfo code.
324 The function must accept six parameters:
325 TODO the todo keyword, as a string
326 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
327 PRIORITY the inlinetask priority, as a string
328 NAME the inlinetask name, as a string.
329 TAGS the inlinetask tags, as a list of strings.
330 CONTENTS the contents of the inlinetask, as a string.
332 The function should return the string to be exported.
334 For example, the variable could be set to the following function
335 in order to mimic default behaviour:
337 \(defun org-texinfo-format-inlinetask \(todo type priority name tags contents\)
338 \"Format an inline task element for Texinfo export.\"
339 \(let ((full-title
340 \(concat
341 \(when todo
342 \(format \"@strong{%s} \" todo))
343 \(when priority (format \"#%c \" priority))
344 title
345 \(when tags
346 \(format \":%s:\"
347 \(mapconcat 'identity tags \":\")))))
348 \(format (concat \"@center %s\n\n\"
349 \"%s\"
350 \"\n\"))
351 full-title contents))"
352 :group 'org-export-texinfo
353 :type 'function)
355 ;;; Src blocks
357 ;; Src Blocks are example blocks, except for LISP
359 ;;; Compilation
361 (defcustom org-texinfo-info-process
362 '("makeinfo %f")
363 "Commands to process a texinfo file to an INFO file.
364 This is list of strings, each of them will be given to the shell
365 as a command. %f in the command will be replaced by the full
366 file name, %b by the file base name \(i.e without extension) and
367 %o by the base directory of the file."
368 :group 'org-export-texinfo
369 :type '(repeat :tag "Shell command sequence"
370 (string :tag "Shell command")))
372 ;;; Constants
373 (defconst org-texinfo-max-toc-depth 4
374 "Maximum depth for creation of detailed menu listings. Beyond
375 this depth texinfo will not recognize the nodes and will cause
376 errors. Left as a constant in case this value ever changes.")
379 ;;; Internal Functions
381 (defun org-texinfo-filter-section-blank-lines (headline back-end info)
382 "Filter controlling number of blank lines after a section."
383 (let ((blanks (make-string 2 ?\n)))
384 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline)))
386 (defun org-texinfo--find-verb-separator (s)
387 "Return a character not used in string S.
388 This is used to choose a separator for constructs like \\verb."
389 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
390 (loop for c across ll
391 when (not (string-match (regexp-quote (char-to-string c)) s))
392 return (char-to-string c))))
394 (defun org-texinfo--make-option-string (options)
395 "Return a comma separated string of keywords and values.
396 OPTIONS is an alist where the key is the options keyword as
397 a string, and the value a list containing the keyword value, or
398 nil."
399 (mapconcat (lambda (pair)
400 (concat (first pair)
401 (when (> (length (second pair)) 0)
402 (concat "=" (second pair)))))
403 options
404 ","))
406 (defun org-texinfo--text-markup (text markup)
407 "Format TEXT depending on MARKUP text markup.
408 See `org-texinfo-text-markup-alist' for details."
409 (let ((fmt (cdr (assq markup org-texinfo-text-markup-alist))))
410 (cond
411 ;; No format string: Return raw text.
412 ((not fmt) text)
413 ((eq 'verb fmt)
414 (let ((separator (org-texinfo--find-verb-separator text)))
415 (concat "@verb{" separator text separator "}")))
416 ((eq 'code fmt)
417 (let ((start 0)
418 (rtn "")
419 char)
420 (while (string-match "[@{}]" text)
421 (setq char (match-string 0 text))
422 (if (> (match-beginning 0) 0)
423 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
424 (setq text (substring text (1+ (match-beginning 0))))
425 (setq char (concat "@" char)
426 rtn (concat rtn char)))
427 (setq text (concat rtn text)
428 fmt "@code{%s}")
429 (format fmt text)))
430 ;; Else use format string.
431 (t (format fmt text)))))
433 (defun org-texinfo--get-node (headline info)
434 "Return node entry associated to HEADLINE.
435 INFO is a plist used as a communication channel."
436 (let ((menu-title (org-element-property :TEXINFO_MENU_TITLE headline)))
437 (org-texinfo--sanitize-menu
438 (replace-regexp-in-string
439 "%" "%%"
440 (if menu-title (org-export-data menu-title info)
441 (org-texinfo--sanitize-headline
442 (org-element-property :title headline) info))))))
444 ;;; Headline sanitizing
446 (defun org-texinfo--sanitize-headline (headline info)
447 "Remove all formatting from the text of a headline for use in
448 node and menu listing."
449 (mapconcat 'identity
450 (org-texinfo--sanitize-headline-contents headline info) " "))
452 (defun org-texinfo--sanitize-headline-contents (headline info)
453 "Retrieve the content of the headline.
455 Any content that can contain further formatting is checked
456 recursively, to ensure that nested content is also properly
457 retrieved."
458 (loop for contents in headline append
459 (cond
460 ;; already a string
461 ((stringp contents)
462 (list (replace-regexp-in-string " $" "" contents)))
463 ;; Is exported as-is (value)
464 ((org-element-map contents '(verbatim code)
465 (lambda (value) (org-element-property :value value)) info))
466 ;; Has content and recurse into the content
467 ((org-element-contents contents)
468 (org-texinfo--sanitize-headline-contents
469 (org-element-contents contents) info)))))
471 ;;; Menu sanitizing
473 (defun org-texinfo--sanitize-menu (title)
474 "Remove invalid characters from TITLE for use in menus and
475 nodes.
477 Based on TEXINFO specifications, the following must be removed:
478 @ { } ( ) : . ,"
479 (replace-regexp-in-string "[@{}():,.]" "" title))
481 ;;; Content sanitizing
483 (defun org-texinfo--sanitize-content (text)
484 "Ensure characters are properly escaped when used in headlines or blocks.
486 Escape characters are: @ { }"
487 (replace-regexp-in-string "\\\([@{}]\\\)" "@\\1" text))
489 ;;; Menu creation
491 (defun org-texinfo--build-menu (tree level info &optional detailed)
492 "Create the @menu/@end menu information from TREE at headline
493 level LEVEL.
495 TREE contains the parse-tree to work with, either of the entire
496 document or of a specific parent headline. LEVEL indicates what
497 level of headlines to look at when generating the menu. INFO is
498 a plist containing contextual information.
500 Detailed determines whether to build a single level of menu, or
501 recurse into all children as well."
502 (let ((menu (org-texinfo--generate-menu-list tree level info))
503 output text-menu)
504 (cond
505 (detailed
506 ;; Looping is done within the menu generation.
507 (setq text-menu (org-texinfo--generate-detailed menu level info)))
509 (setq text-menu (org-texinfo--generate-menu-items menu info))))
510 (when text-menu
511 (setq output (org-texinfo--format-menu text-menu))
512 (mapconcat 'identity output "\n"))))
514 (defun org-texinfo--generate-detailed (menu level info)
515 "Generate a detailed listing of all subheadings within MENU starting at LEVEL.
517 MENU is the parse-tree to work with. LEVEL is the starting level
518 for the menu headlines and from which recursion occurs. INFO is
519 a plist containing contextual information."
520 (when level
521 (let ((max-depth (min org-texinfo-max-toc-depth
522 (plist-get info :headline-levels))))
523 (when (> max-depth level)
524 (loop for headline in menu append
525 (let* ((title (org-texinfo--menu-headlines headline info))
526 ;; Create list of menu entries for the next level
527 (sublist (org-texinfo--generate-menu-list
528 headline (1+ level) info))
529 ;; Generate the menu items for that level. If
530 ;; there are none omit that heading completely,
531 ;; otherwise join the title to it's related entries.
532 (submenu (if (org-texinfo--generate-menu-items sublist info)
533 (append (list title)
534 (org-texinfo--generate-menu-items sublist info))
535 'nil))
536 ;; Start the process over the next level down.
537 (recursion (org-texinfo--generate-detailed sublist (1+ level) info)))
538 (setq recursion (append submenu recursion))
539 recursion))))))
541 (defun org-texinfo--generate-menu-list (tree level info)
542 "Generate the list of headlines that are within a given level
543 of the tree for further formatting.
545 TREE is the parse-tree containing the headlines. LEVEL is the
546 headline level to generate a list of. INFO is a plist holding
547 contextual information."
548 (org-element-map tree 'headline
549 (lambda (head)
550 (and (= (org-export-get-relative-level head info) level)
551 ;; Do not take note of footnotes or copying headlines.
552 (not (org-element-property :COPYING head))
553 (not (org-element-property :footnote-section-p head))
554 ;; Collect headline.
555 head))
556 info))
558 (defun org-texinfo--generate-menu-items (items info)
559 "Generate a list of headline information from the listing ITEMS.
561 ITEMS is a list of the headlines to be converted into entries.
562 INFO is a plist containing contextual information.
564 Returns a list containing the following information from each
565 headline: length, title, description. This is used to format the
566 menu using `org-texinfo--format-menu'."
567 (loop for headline in items collect
568 (let* ((menu-title (org-texinfo--sanitize-menu
569 (org-export-data
570 (org-element-property :TEXINFO_MENU_TITLE headline)
571 info)))
572 (title (org-texinfo--sanitize-menu
573 (org-texinfo--sanitize-headline
574 (org-element-property :title headline) info)))
575 (descr (org-export-data
576 (org-element-property :DESCRIPTION headline)
577 info))
578 (menu-entry (if (string= "" menu-title) title menu-title))
579 (len (length menu-entry))
580 (output (list len menu-entry descr)))
581 output)))
583 (defun org-texinfo--menu-headlines (headline info)
584 "Retrieve the title from HEADLINE.
586 INFO is a plist holding contextual information.
588 Return the headline as a list of (length title description) with
589 length of -1 and nil description. This is used in
590 `org-texinfo--format-menu' to identify headlines as opposed to
591 entries."
592 (let ((title (org-export-data
593 (org-element-property :title headline) info)))
594 (list -1 title 'nil)))
596 (defun org-texinfo--format-menu (text-menu)
597 "Format the TEXT-MENU items to be properly printed in the menu.
599 Each entry in the menu should be provided as (length title
600 description).
602 Headlines in the detailed menu are given length -1 to ensure they
603 are never confused with other entries. They also have no
604 description.
606 Other menu items are output as:
607 Title:: description
609 With the spacing between :: and description based on the length
610 of the longest menu entry."
612 (let* ((lengths (mapcar 'car text-menu))
613 (max-length (apply 'max lengths))
614 output)
615 (setq output
616 (mapcar (lambda (name)
617 (let* ((title (nth 1 name))
618 (desc (nth 2 name))
619 (length (nth 0 name)))
620 (if (> length -1)
621 (concat "* " title ":: "
622 (make-string
623 (- (+ 3 max-length) length)
624 ?\s)
625 (if desc
626 (concat desc)))
627 (concat "\n" title "\n"))))
628 text-menu))
629 output))
631 ;;; Template
633 (defun org-texinfo-template (contents info)
634 "Return complete document string after Texinfo conversion.
635 CONTENTS is the transcoded contents string. INFO is a plist
636 holding export options."
637 (let* ((title (org-export-data (plist-get info :title) info))
638 (info-filename (or (plist-get info :texinfo-filename)
639 (file-name-nondirectory
640 (org-export-output-file-name ".info"))))
641 (author (org-export-data (plist-get info :author) info))
642 (texinfo-header (plist-get info :texinfo-header))
643 (texinfo-post-header (plist-get info :texinfo-post-header))
644 (subtitle (plist-get info :subtitle))
645 (subauthor (plist-get info :subauthor))
646 (class (plist-get info :texinfo-class))
647 (header (nth 1 (assoc class org-texinfo-classes)))
648 (copying
649 (org-element-map (plist-get info :parse-tree) 'headline
650 (lambda (hl) (and (org-element-property :COPYING hl) hl)) info t))
651 (dircat (plist-get info :texinfo-dircat))
652 (dirtitle (plist-get info :texinfo-dirtitle))
653 (dirdesc (plist-get info :texinfo-dirdesc))
654 ;; Spacing to align description (column 32 - 3 for `* ' and
655 ;; `.' in text.
656 (dirspacing (- 29 (length dirtitle)))
657 (menu (org-texinfo-make-menu info 'main))
658 (detail-menu (org-texinfo-make-menu info 'detailed)))
659 (concat
660 ;; Header
661 header "\n"
662 "@c %**start of header\n"
663 ;; Filename and Title
664 "@setfilename " info-filename "\n"
665 "@settitle " title "\n"
666 "\n\n"
667 "@c Version and Contact Info\n"
668 "@set AUTHOR " author "\n"
670 ;; Additional Header Options set by `#+TEXINFO_HEADER
671 (if texinfo-header
672 (concat "\n"
673 texinfo-header
674 "\n"))
676 "@c %**end of header\n"
677 "@finalout\n"
678 "\n\n"
680 ;; Additional Header Options set by #+TEXINFO_POST_HEADER
681 (if texinfo-post-header
682 (concat "\n"
683 texinfo-post-header
684 "\n"))
686 ;; Copying
687 "@copying\n"
688 ;; Only export the content of the headline, do not need the
689 ;; initial headline.
690 (org-export-data (nth 2 copying) info)
691 "@end copying\n"
692 "\n\n"
694 ;; Info directory information
695 ;; Only supply if both title and category are provided
696 (if (and dircat dirtitle)
697 (concat "@dircategory " dircat "\n"
698 "@direntry\n"
699 "* " dirtitle "."
700 (make-string dirspacing ?\s)
701 dirdesc "\n"
702 "@end direntry\n"))
703 "\n\n"
705 ;; Title
706 "@titlepage\n"
707 "@title " title "\n\n"
708 (if subtitle
709 (concat "@subtitle " subtitle "\n"))
710 "@author " author "\n"
711 (if subauthor
712 (concat subauthor "\n"))
713 "\n"
714 "@c The following two commands start the copyright page.\n"
715 "@page\n"
716 "@vskip 0pt plus 1filll\n"
717 "@insertcopying\n"
718 "@end titlepage\n\n"
719 "@c Output the table of contents at the beginning.\n"
720 "@contents\n\n"
722 ;; Configure Top Node when not for Tex
723 "@ifnottex\n"
724 "@node Top\n"
725 "@top " title " Manual\n"
726 "@insertcopying\n"
727 "@end ifnottex\n\n"
729 ;; Do not output menus if they are empty
730 (if menu
731 ;; Menu
732 (concat "@menu\n"
733 menu
734 "\n\n"
735 ;; Detailed Menu
736 (if detail-menu
737 (concat "@detailmenu\n"
738 " --- The Detailed Node Listing ---\n"
739 detail-menu
740 "\n\n"
741 "@end detailmenu\n"))
742 "@end menu\n"))
743 "\n\n"
745 ;; Document's body.
746 contents
747 "\n"
748 ;; Creator.
749 (let ((creator-info (plist-get info :with-creator)))
750 (cond
751 ((not creator-info) "")
752 ((eq creator-info 'comment)
753 (format "@c %s\n" (plist-get info :creator)))
754 (t (concat (plist-get info :creator) "\n"))))
755 ;; Document end.
756 "\n@bye")))
760 ;;; Transcode Functions
762 ;;; Bold
764 (defun org-texinfo-bold (bold contents info)
765 "Transcode BOLD from Org to Texinfo.
766 CONTENTS is the text with bold markup. INFO is a plist holding
767 contextual information."
768 (org-texinfo--text-markup contents 'bold))
770 ;;; Center Block
772 (defun org-texinfo-center-block (center-block contents info)
773 "Transcode a CENTER-BLOCK element from Org to Texinfo.
774 CONTENTS holds the contents of the block. INFO is a plist used
775 as a communication channel."
776 contents)
778 ;;; Clock
780 (defun org-texinfo-clock (clock contents info)
781 "Transcode a CLOCK element from Org to Texinfo.
782 CONTENTS is nil. INFO is a plist holding contextual
783 information."
784 (concat
785 "@noindent"
786 (format "@strong{%s} " org-clock-string)
787 (format org-texinfo-inactive-timestamp-format
788 (concat (org-translate-time
789 (org-element-property :raw-value
790 (org-element-property :value clock)))
791 (let ((time (org-element-property :duration clock)))
792 (and time (format " (%s)" time)))))
793 "@*"))
795 ;;; Code
797 (defun org-texinfo-code (code contents info)
798 "Transcode a CODE object from Org to Texinfo.
799 CONTENTS is nil. INFO is a plist used as a communication
800 channel."
801 (org-texinfo--text-markup (org-element-property :value code) 'code))
803 ;;; Comment
805 (defun org-texinfo-comment (comment contents info)
806 "Transcode a COMMENT object from Org to Texinfo.
807 CONTENTS is the text in the comment. INFO is a plist holding
808 contextual information."
809 (org-texinfo--text-markup (org-element-property :value comment) 'comment))
811 ;;; Comment Block
813 (defun org-texinfo-comment-block (comment-block contents info)
814 "Transcode a COMMENT-BLOCK object from Org to Texinfo.
815 CONTENTS is the text within the block. INFO is a plist holding
816 contextual information."
817 (format "@ignore\n%s@end ignore" (org-element-property :value comment-block)))
819 ;;; Drawer
821 (defun org-texinfo-drawer (drawer contents info)
822 "Transcode a DRAWER element from Org to Texinfo.
823 CONTENTS holds the contents of the block. INFO is a plist
824 holding contextual information."
825 (let* ((name (org-element-property :drawer-name drawer))
826 (output (if (functionp org-texinfo-format-drawer-function)
827 (funcall org-texinfo-format-drawer-function
828 name contents)
829 ;; If there's no user defined function: simply
830 ;; display contents of the drawer.
831 contents)))
832 output))
834 ;;; Dynamic Block
836 (defun org-texinfo-dynamic-block (dynamic-block contents info)
837 "Transcode a DYNAMIC-BLOCK element from Org to Texinfo.
838 CONTENTS holds the contents of the block. INFO is a plist
839 holding contextual information. See `org-export-data'."
840 contents)
842 ;;; Entity
844 (defun org-texinfo-entity (entity contents info)
845 "Transcode an ENTITY object from Org to Texinfo.
846 CONTENTS are the definition itself. INFO is a plist holding
847 contextual information."
848 (let ((ent (org-element-property :latex entity)))
849 (if (org-element-property :latex-math-p entity) (format "@math{%s}" ent) ent)))
851 ;;; Example Block
853 (defun org-texinfo-example-block (example-block contents info)
854 "Transcode an EXAMPLE-BLOCK element from Org to Texinfo.
855 CONTENTS is nil. INFO is a plist holding contextual
856 information."
857 (format "@verbatim\n%s@end verbatim"
858 (org-export-format-code-default example-block info)))
860 ;;; Export Block
862 (defun org-texinfo-export-block (export-block contents info)
863 "Transcode a EXPORT-BLOCK element from Org to Texinfo.
864 CONTENTS is nil. INFO is a plist holding contextual information."
865 (when (string= (org-element-property :type export-block) "TEXINFO")
866 (org-remove-indentation (org-element-property :value export-block))))
868 ;;; Export Snippet
870 (defun org-texinfo-export-snippet (export-snippet contents info)
871 "Transcode a EXPORT-SNIPPET object from Org to Texinfo.
872 CONTENTS is nil. INFO is a plist holding contextual information."
873 (when (eq (org-export-snippet-backend export-snippet) 'texinfo)
874 (org-element-property :value export-snippet)))
876 ;;; Fixed Width
878 (defun org-texinfo-fixed-width (fixed-width contents info)
879 "Transcode a FIXED-WIDTH element from Org to Texinfo.
880 CONTENTS is nil. INFO is a plist holding contextual information."
881 (format "@example\n%s\n@end example"
882 (org-remove-indentation
883 (org-texinfo--sanitize-content
884 (org-element-property :value fixed-width)))))
886 ;;; Footnote Reference
889 (defun org-texinfo-footnote-reference (footnote contents info)
890 "Create a footnote reference for FOOTNOTE.
892 FOOTNOTE is the footnote to define. CONTENTS is nil. INFO is a
893 plist holding contextual information."
894 (let ((def (org-export-get-footnote-definition footnote info)))
895 (format "@footnote{%s}"
896 (org-trim (org-export-data def info)))))
898 ;;; Headline
900 (defun org-texinfo-headline (headline contents info)
901 "Transcode an HEADLINE element from Org to Texinfo.
902 CONTENTS holds the contents of the headline. INFO is a plist
903 holding contextual information."
904 (let* ((class (plist-get info :texinfo-class))
905 (level (org-export-get-relative-level headline info))
906 (numberedp (org-export-numbered-headline-p headline info))
907 (class-sectionning (assoc class org-texinfo-classes))
908 ;; Find the index type, if any
909 (index (org-element-property :index headline))
910 ;; Retrieve headline text
911 (text (org-texinfo--sanitize-headline
912 (org-element-property :title headline) info))
913 ;; Create node info, to insert it before section formatting.
914 ;; Use custom menu title if present
915 (node (format "@node %s\n" (org-texinfo--get-node headline info)))
916 ;; Menus must be generated with first child, otherwise they
917 ;; will not nest properly
918 (menu (let* ((first (org-export-first-sibling-p headline info))
919 (parent (org-export-get-parent-headline headline))
920 (title (org-texinfo--sanitize-headline
921 (org-element-property :title parent) info))
922 heading listing
923 (tree (plist-get info :parse-tree)))
924 (if first
925 (org-element-map (plist-get info :parse-tree) 'headline
926 (lambda (ref)
927 (if (member title (org-element-property :title ref))
928 (push ref heading)))
929 info t))
930 (setq listing (org-texinfo--build-menu
931 (car heading) level info))
932 (if listing
933 (setq listing (replace-regexp-in-string
934 "%" "%%" listing)
935 listing (format
936 "\n@menu\n%s\n@end menu\n\n" listing))
937 'nil)))
938 ;; Section formatting will set two placeholders: one for the
939 ;; title and the other for the contents.
940 (section-fmt
941 (let ((sec (if (and (symbolp (nth 2 class-sectionning))
942 (fboundp (nth 2 class-sectionning)))
943 (funcall (nth 2 class-sectionning) level numberedp)
944 (nth (1+ level) class-sectionning))))
945 (cond
946 ;; No section available for that LEVEL.
947 ((not sec) nil)
948 ;; Section format directly returned by a function.
949 ((stringp sec) sec)
950 ;; (numbered-section . unnumbered-section)
951 ((not (consp (cdr sec)))
952 ;; If an index, always unnumbered
953 (if index
954 (concat menu node (cdr sec) "\n%s")
955 ;; Otherwise number as needed.
956 (concat menu node
957 (funcall
958 (if numberedp #'car #'cdr) sec) "\n%s"))))))
959 (todo
960 (and (plist-get info :with-todo-keywords)
961 (let ((todo (org-element-property :todo-keyword headline)))
962 (and todo (org-export-data todo info)))))
963 (todo-type (and todo (org-element-property :todo-type headline)))
964 (tags (and (plist-get info :with-tags)
965 (org-export-get-tags headline info)))
966 (priority (and (plist-get info :with-priority)
967 (org-element-property :priority headline)))
968 ;; Create the headline text along with a no-tag version. The
969 ;; latter is required to remove tags from table of contents.
970 (full-text (org-texinfo--sanitize-content
971 (if (functionp org-texinfo-format-headline-function)
972 ;; User-defined formatting function.
973 (funcall org-texinfo-format-headline-function
974 todo todo-type priority text tags)
975 ;; Default formatting.
976 (concat
977 (when todo
978 (format "@strong{%s} " todo))
979 (when priority (format "@emph{#%s} " priority))
980 text
981 (when tags
982 (format ":%s:"
983 (mapconcat 'identity tags ":")))))))
984 (full-text-no-tag
985 (org-texinfo--sanitize-content
986 (if (functionp org-texinfo-format-headline-function)
987 ;; User-defined formatting function.
988 (funcall org-texinfo-format-headline-function
989 todo todo-type priority text nil)
990 ;; Default formatting.
991 (concat
992 (when todo (format "@strong{%s} " todo))
993 (when priority (format "@emph{#%c} " priority))
994 text))))
995 (pre-blanks
996 (make-string (org-element-property :pre-blank headline) 10)))
997 (cond
998 ;; Case 1: This is a footnote section: ignore it.
999 ((org-element-property :footnote-section-p headline) nil)
1000 ;; Case 2: This is the `copying' section: ignore it
1001 ;; This is used elsewhere.
1002 ((org-element-property :COPYING headline) nil)
1003 ;; Case 3: An index. If it matches one of the known indexes,
1004 ;; print it as such following the contents, otherwise
1005 ;; print the contents and leave the index up to the user.
1006 (index
1007 (format
1008 section-fmt full-text
1009 (concat pre-blanks contents "\n"
1010 (if (member index '("cp" "fn" "ky" "pg" "tp" "vr"))
1011 (concat "@printindex " index)))))
1012 ;; Case 4: This is a deep sub-tree: export it as a list item.
1013 ;; Also export as items headlines for which no section
1014 ;; format has been found.
1015 ((or (not section-fmt) (org-export-low-level-p headline info))
1016 ;; Build the real contents of the sub-tree.
1017 (let ((low-level-body
1018 (concat
1019 ;; If the headline is the first sibling, start a list.
1020 (when (org-export-first-sibling-p headline info)
1021 (format "@%s\n" (if numberedp 'enumerate 'itemize)))
1022 ;; Itemize headline
1023 "@item\n" full-text "\n" pre-blanks contents)))
1024 ;; If headline is not the last sibling simply return
1025 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1026 ;; blank line.
1027 (if (not (org-export-last-sibling-p headline info)) low-level-body
1028 (replace-regexp-in-string
1029 "[ \t\n]*\\'"
1030 (format "\n@end %s" (if numberedp 'enumerate 'itemize))
1031 low-level-body))))
1032 ;; Case 5: Standard headline. Export it as a section.
1034 (cond
1035 ((not (and tags (eq (plist-get info :with-tags) 'not-in-toc)))
1036 ;; Regular section. Use specified format string.
1037 (format (replace-regexp-in-string "%]" "%%]" section-fmt) full-text
1038 (concat pre-blanks contents)))
1039 ((string-match "\\`@\\(.*?\\){" section-fmt)
1040 ;; If tags should be removed from table of contents, insert
1041 ;; title without tags as an alternative heading in sectioning
1042 ;; command.
1043 (format (replace-match (concat (match-string 1 section-fmt) "[%s]")
1044 nil nil section-fmt 1)
1045 ;; Replace square brackets with parenthesis since
1046 ;; square brackets are not supported in optional
1047 ;; arguments.
1048 (replace-regexp-in-string
1049 "\\[" "("
1050 (replace-regexp-in-string
1051 "\\]" ")"
1052 full-text-no-tag))
1053 full-text
1054 (concat pre-blanks contents)))
1056 ;; Impossible to add an alternative heading. Fallback to
1057 ;; regular sectioning format string.
1058 (format (replace-regexp-in-string "%]" "%%]" section-fmt) full-text
1059 (concat pre-blanks contents))))))))
1061 ;;; Inline Src Block
1063 (defun org-texinfo-inline-src-block (inline-src-block contents info)
1064 "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
1065 CONTENTS holds the contents of the item. INFO is a plist holding
1066 contextual information."
1067 (let* ((code (org-element-property :value inline-src-block))
1068 (separator (org-texinfo--find-verb-separator code)))
1069 (concat "@verb{" separator code separator "}")))
1071 ;;; Inlinetask
1073 (defun org-texinfo-inlinetask (inlinetask contents info)
1074 "Transcode an INLINETASK element from Org to Texinfo.
1075 CONTENTS holds the contents of the block. INFO is a plist
1076 holding contextual information."
1077 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1078 (todo (and (plist-get info :with-todo-keywords)
1079 (let ((todo (org-element-property :todo-keyword inlinetask)))
1080 (and todo (org-export-data todo info)))))
1081 (todo-type (org-element-property :todo-type inlinetask))
1082 (tags (and (plist-get info :with-tags)
1083 (org-export-get-tags inlinetask info)))
1084 (priority (and (plist-get info :with-priority)
1085 (org-element-property :priority inlinetask))))
1086 ;; If `org-texinfo-format-inlinetask-function' is provided, call it
1087 ;; with appropriate arguments.
1088 (if (functionp org-texinfo-format-inlinetask-function)
1089 (funcall org-texinfo-format-inlinetask-function
1090 todo todo-type priority title tags contents)
1091 ;; Otherwise, use a default template.
1092 (let ((full-title
1093 (concat
1094 (when todo (format "@strong{%s} " todo))
1095 (when priority (format "#%c " priority))
1096 title
1097 (when tags (format ":%s:"
1098 (mapconcat 'identity tags ":"))))))
1099 (format (concat "@center %s\n\n"
1100 "%s"
1101 "\n")
1102 full-title contents)))))
1104 ;;; Italic
1106 (defun org-texinfo-italic (italic contents info)
1107 "Transcode ITALIC from Org to Texinfo.
1108 CONTENTS is the text with italic markup. INFO is a plist holding
1109 contextual information."
1110 (org-texinfo--text-markup contents 'italic))
1112 ;;; Item
1114 (defun org-texinfo-item (item contents info)
1115 "Transcode an ITEM element from Org to Texinfo.
1116 CONTENTS holds the contents of the item. INFO is a plist holding
1117 contextual information."
1118 (let* ((tag (org-element-property :tag item))
1119 (desc (org-export-data tag info)))
1120 (concat "\n@item " (if tag desc) "\n"
1121 (org-trim contents) "\n")))
1123 ;;; Keyword
1125 (defun org-texinfo-keyword (keyword contents info)
1126 "Transcode a KEYWORD element from Org to Texinfo.
1127 CONTENTS is nil. INFO is a plist holding contextual information."
1128 (let ((key (org-element-property :key keyword))
1129 (value (org-element-property :value keyword)))
1130 (cond
1131 ((string= key "TEXINFO") value)
1132 ((string= key "CINDEX") (format "@cindex %s" value))
1133 ((string= key "FINDEX") (format "@findex %s" value))
1134 ((string= key "KINDEX") (format "@kindex %s" value))
1135 ((string= key "PINDEX") (format "@pindex %s" value))
1136 ((string= key "TINDEX") (format "@tindex %s" value))
1137 ((string= key "VINDEX") (format "@vindex %s" value)))))
1139 ;;; Line Break
1141 (defun org-texinfo-line-break (line-break contents info)
1142 "Transcode a LINE-BREAK object from Org to Texinfo.
1143 CONTENTS is nil. INFO is a plist holding contextual information."
1144 "@*\n")
1146 ;;; Link
1148 (defun org-texinfo-link (link desc info)
1149 "Transcode a LINK object from Org to Texinfo.
1151 DESC is the description part of the link, or the empty string.
1152 INFO is a plist holding contextual information. See
1153 `org-export-data'."
1154 (let* ((type (org-element-property :type link))
1155 (raw-path (org-element-property :path link))
1156 ;; Ensure DESC really exists, or set it to nil.
1157 (desc (and (not (string= desc "")) desc))
1158 (path (cond
1159 ((member type '("http" "https" "ftp"))
1160 (concat type ":" raw-path))
1161 ((string= type "file")
1162 (if (file-name-absolute-p raw-path)
1163 (concat "file://" (expand-file-name raw-path))
1164 (concat "file://" raw-path)))
1165 (t raw-path)))
1166 (email (if (string= type "mailto")
1167 (let ((text (replace-regexp-in-string
1168 "@" "@@" raw-path)))
1169 (concat text (if desc (concat "," desc))))))
1170 protocol)
1171 (cond
1172 ;; Links pointing to an headline: Find destination and build
1173 ;; appropriate referencing command.
1174 ((member type '("custom-id" "id"))
1175 (let ((destination (org-export-resolve-id-link link info)))
1176 (case (org-element-type destination)
1177 ;; Id link points to an external file.
1178 (plain-text
1179 (if desc (format "@uref{file://%s,%s}" destination desc)
1180 (format "@uref{file://%s}" destination)))
1181 ;; LINK points to an headline. Use the headline as the NODE target
1182 (headline
1183 (format "@ref{%s,%s}"
1184 (org-texinfo--get-node destination info)
1185 (or desc "")))
1186 (otherwise
1187 (let ((path (org-export-solidify-link-text path)))
1188 (if (not desc) (format "@ref{%s}" path)
1189 (format "@ref{%s,,%s}" path desc)))))))
1190 ((member type '("info"))
1191 (let* ((info-path (split-string path ":"))
1192 (info-manual (car info-path))
1193 (info-node (or (cadr info-path) "top"))
1194 (title (or desc "")))
1195 (format "@ref{%s,%s,,%s,}" info-node title info-manual)))
1196 ((member type '("fuzzy"))
1197 (let ((destination (org-export-resolve-fuzzy-link link info)))
1198 (case (org-element-type destination)
1199 ;; Id link points to an external file.
1200 (plain-text
1201 (if desc (format "@uref{file://%s,%s}" destination desc)
1202 (format "@uref{file://%s}" destination)))
1203 ;; LINK points to an headline. Use the headline as the NODE target
1204 (headline
1205 (format "@ref{%s,%s}"
1206 (org-texinfo--get-node destination info)
1207 (or desc "")))
1208 (otherwise
1209 (let ((path (org-export-solidify-link-text path)))
1210 (if (not desc) (format "@ref{%s}" path)
1211 (format "@ref{%s,,%s}" path desc)))))))
1212 ;; Special case for email addresses
1213 (email
1214 (format "@email{%s}" email))
1215 ;; External link with a description part.
1216 ((and path desc) (format "@uref{%s,%s}" path desc))
1217 ;; External link without a description part.
1218 (path (format "@uref{%s}" path))
1219 ;; No path, only description. Try to do something useful.
1220 (t (format org-texinfo-link-with-unknown-path-format desc)))))
1223 ;;; Menu
1225 (defun org-texinfo-make-menu (info level)
1226 "Create the menu for inclusion in the texifo document.
1228 INFO is the parsed buffer that contains the headlines. LEVEL
1229 determines whether to make the main menu, or the detailed menu.
1231 This is only used for generating the primary menu. In-Node menus
1232 are generated directly."
1233 (let ((parse (plist-get info :parse-tree)))
1234 (cond
1235 ;; Generate the main menu
1236 ((eq level 'main) (org-texinfo--build-menu parse 1 info))
1237 ;; Generate the detailed (recursive) menu
1238 ((eq level 'detailed)
1239 ;; Requires recursion
1240 ;;(org-texinfo--build-detailed-menu parse top info)
1241 (org-texinfo--build-menu parse 1 info 'detailed)))))
1243 ;;; Paragraph
1245 (defun org-texinfo-paragraph (paragraph contents info)
1246 "Transcode a PARAGRAPH element from Org to Texinfo.
1247 CONTENTS is the contents of the paragraph, as a string. INFO is
1248 the plist used as a communication channel."
1249 contents)
1251 ;;; Plain List
1253 (defun org-texinfo-plain-list (plain-list contents info)
1254 "Transcode a PLAIN-LIST element from Org to Texinfo.
1255 CONTENTS is the contents of the list. INFO is a plist holding
1256 contextual information."
1257 (let* ((attr (org-export-read-attribute :attr_texinfo plain-list))
1258 (indic (or (plist-get attr :indic)
1259 org-texinfo-def-table-markup))
1260 (type (org-element-property :type plain-list))
1261 (table-type (or (plist-get attr :table-type)
1262 "table"))
1263 ;; Ensure valid texinfo table type.
1264 (table-type (if (memq table-type '("table" "ftable" "vtable"))
1265 table-type
1266 "table"))
1267 (list-type (cond
1268 ((eq type 'ordered) "enumerate")
1269 ((eq type 'unordered) "itemize")
1270 ((eq type 'descriptive) table-type))))
1271 (format "@%s%s\n@end %s"
1272 (if (eq type 'descriptive)
1273 (concat list-type " " indic)
1274 list-type)
1275 contents
1276 list-type)))
1278 ;;; Plain Text
1280 (defun org-texinfo-plain-text (text info)
1281 "Transcode a TEXT string from Org to Texinfo.
1282 TEXT is the string to transcode. INFO is a plist holding
1283 contextual information."
1284 ;; First protect @, { and }.
1285 (let ((output (org-texinfo--sanitize-content text)))
1286 ;; Activate smart quotes. Be sure to provide original TEXT string
1287 ;; since OUTPUT may have been modified.
1288 (when (plist-get info :with-smart-quotes)
1289 (setq output
1290 (org-export-activate-smart-quotes output :texinfo info text)))
1291 ;; LaTeX into @LaTeX{} and TeX into @TeX{}
1292 (let ((case-fold-search nil)
1293 (start 0))
1294 (while (string-match "\\(\\(?:La\\)?TeX\\)" output start)
1295 (setq output (replace-match
1296 (format "@%s{}" (match-string 1 output)) nil t output)
1297 start (match-end 0))))
1298 ;; Convert special strings.
1299 (when (plist-get info :with-special-strings)
1300 (while (string-match (regexp-quote "...") output)
1301 (setq output (replace-match "@dots{}" nil t output))))
1302 ;; Handle break preservation if required.
1303 (when (plist-get info :preserve-breaks)
1304 (setq output (replace-regexp-in-string
1305 "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" output)))
1306 ;; Return value.
1307 output))
1309 ;;; Planning
1311 (defun org-texinfo-planning (planning contents info)
1312 "Transcode a PLANNING element from Org to Texinfo.
1313 CONTENTS is nil. INFO is a plist holding contextual
1314 information."
1315 (concat
1316 "@noindent"
1317 (mapconcat
1318 'identity
1319 (delq nil
1320 (list
1321 (let ((closed (org-element-property :closed planning)))
1322 (when closed
1323 (concat
1324 (format "@strong{%s} " org-closed-string)
1325 (format org-texinfo-inactive-timestamp-format
1326 (org-translate-time
1327 (org-element-property :raw-value closed))))))
1328 (let ((deadline (org-element-property :deadline planning)))
1329 (when deadline
1330 (concat
1331 (format "@strong{%s} " org-deadline-string)
1332 (format org-texinfo-active-timestamp-format
1333 (org-translate-time
1334 (org-element-property :raw-value deadline))))))
1335 (let ((scheduled (org-element-property :scheduled planning)))
1336 (when scheduled
1337 (concat
1338 (format "@strong{%s} " org-scheduled-string)
1339 (format org-texinfo-active-timestamp-format
1340 (org-translate-time
1341 (org-element-property :raw-value scheduled))))))))
1342 " ")
1343 "@*"))
1345 ;;; Property Drawer
1347 (defun org-texinfo-property-drawer (property-drawer contents info)
1348 "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
1349 CONTENTS is nil. INFO is a plist holding contextual
1350 information."
1351 ;; The property drawer isn't exported but we want separating blank
1352 ;; lines nonetheless.
1355 ;;; Quote Block
1357 (defun org-texinfo-quote-block (quote-block contents info)
1358 "Transcode a QUOTE-BLOCK element from Org to Texinfo.
1359 CONTENTS holds the contents of the block. INFO is a plist
1360 holding contextual information."
1361 (let* ((title (org-element-property :name quote-block))
1362 (start-quote (concat "@quotation"
1363 (if title
1364 (format " %s" title)))))
1365 (format "%s\n%s@end quotation" start-quote contents)))
1367 ;;; Quote Section
1369 (defun org-texinfo-quote-section (quote-section contents info)
1370 "Transcode a QUOTE-SECTION element from Org to Texinfo.
1371 CONTENTS is nil. INFO is a plist holding contextual information."
1372 (let ((value (org-remove-indentation
1373 (org-element-property :value quote-section))))
1374 (when value (format "@verbatim\n%s@end verbatim" value))))
1376 ;;; Radio Target
1378 (defun org-texinfo-radio-target (radio-target text info)
1379 "Transcode a RADIO-TARGET object from Org to Texinfo.
1380 TEXT is the text of the target. INFO is a plist holding
1381 contextual information."
1382 (format "@anchor{%s}%s"
1383 (org-export-solidify-link-text
1384 (org-element-property :value radio-target))
1385 text))
1387 ;;; Section
1389 (defun org-texinfo-section (section contents info)
1390 "Transcode a SECTION element from Org to Texinfo.
1391 CONTENTS holds the contents of the section. INFO is a plist
1392 holding contextual information."
1393 contents)
1395 ;;; Special Block
1397 (defun org-texinfo-special-block (special-block contents info)
1398 "Transcode a SPECIAL-BLOCK element from Org to Texinfo.
1399 CONTENTS holds the contents of the block. INFO is a plist used
1400 as a communication channel."
1401 contents)
1403 ;;; Src Block
1405 (defun org-texinfo-src-block (src-block contents info)
1406 "Transcode a SRC-BLOCK element from Org to Texinfo.
1407 CONTENTS holds the contents of the item. INFO is a plist holding
1408 contextual information."
1409 (let* ((lang (org-element-property :language src-block))
1410 (lisp-p (string-match-p "lisp" lang)))
1411 (cond
1412 ;; Case 1. Lisp Block
1413 (lisp-p
1414 (format "@lisp\n%s@end lisp"
1415 (org-export-format-code-default src-block info)))
1416 ;; Case 2. Other blocks
1418 (format "@example\n%s@end example"
1419 (org-export-format-code-default src-block info))))))
1421 ;;; Statistics Cookie
1423 (defun org-texinfo-statistics-cookie (statistics-cookie contents info)
1424 "Transcode a STATISTICS-COOKIE object from Org to Texinfo.
1425 CONTENTS is nil. INFO is a plist holding contextual information."
1426 (org-element-property :value statistics-cookie))
1428 ;;; Subscript
1430 (defun org-texinfo-subscript (subscript contents info)
1431 "Transcode a SUBSCRIPT object from Org to Texinfo.
1432 CONTENTS is the contents of the object. INFO is a plist holding
1433 contextual information."
1434 (format "@math{_%s}" contents))
1436 ;;; Superscript
1438 (defun org-texinfo-superscript (superscript contents info)
1439 "Transcode a SUPERSCRIPT object from Org to Texinfo.
1440 CONTENTS is the contents of the object. INFO is a plist holding
1441 contextual information."
1442 (format "@math{^%s}" contents))
1444 ;;; Table
1446 ;; `org-texinfo-table' is the entry point for table transcoding. It
1447 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
1448 ;; delegates the job to either `org-texinfo-table--table.el-table' or
1449 ;; `org-texinfo-table--org-table' functions, depending of the type of
1450 ;; the table.
1452 ;; `org-texinfo-table--align-string' is a subroutine used to build
1453 ;; alignment string for Org tables.
1455 (defun org-texinfo-table (table contents info)
1456 "Transcode a TABLE element from Org to Texinfo.
1457 CONTENTS is the contents of the table. INFO is a plist holding
1458 contextual information."
1459 (cond
1460 ;; Case 1: verbatim table.
1461 ((or org-texinfo-tables-verbatim
1462 (let ((attr (mapconcat 'identity
1463 (org-element-property :attr_latex table)
1464 " ")))
1465 (and attr (string-match "\\<verbatim\\>" attr))))
1466 (format "@verbatim \n%s\n@end verbatim"
1467 ;; Re-create table, without affiliated keywords.
1468 (org-trim
1469 (org-element-interpret-data
1470 `(table nil ,@(org-element-contents table))))))
1471 ;; Case 2: table.el table. Convert it using appropriate tools.
1472 ((eq (org-element-property :type table) 'table.el)
1473 (org-texinfo-table--table.el-table table contents info))
1474 ;; Case 3: Standard table.
1475 (t (org-texinfo-table--org-table table contents info))))
1477 (defun org-texinfo-table-column-widths (table info)
1478 "Determine the largest table cell in each column to process alignment.
1480 TABLE is the table element to transcode. INFO is a plist used as
1481 a communication channel."
1482 (let* ((rows (org-element-map table 'table-row 'identity info))
1483 (collected (loop for row in rows collect
1484 (org-element-map row 'table-cell 'identity info)))
1485 (number-cells (length (car collected)))
1486 cells counts)
1487 (loop for row in collected do
1488 (push (mapcar (lambda (ref)
1489 (let* ((start (org-element-property :contents-begin ref))
1490 (end (org-element-property :contents-end ref))
1491 (length (- end start)))
1492 length)) row) cells))
1493 (setq cells (org-remove-if 'null cells))
1494 (push (loop for count from 0 to (- number-cells 1) collect
1495 (loop for item in cells collect
1496 (nth count item))) counts)
1497 (mapconcat (lambda (size)
1498 (make-string size ?a)) (mapcar (lambda (ref)
1499 (apply 'max `,@ref)) (car counts))
1500 "} {")))
1502 (defun org-texinfo-table--org-table (table contents info)
1503 "Return appropriate Texinfo code for an Org table.
1505 TABLE is the table type element to transcode. CONTENTS is its
1506 contents, as a string. INFO is a plist used as a communication
1507 channel.
1509 This function assumes TABLE has `org' as its `:type' attribute."
1510 (let* ((attr (org-export-read-attribute :attr_texinfo table))
1511 (col-width (plist-get attr :columns))
1512 (columns (if col-width
1513 (format "@columnfractions %s"
1514 col-width)
1515 (format "{%s}"
1516 (org-texinfo-table-column-widths
1517 table info)))))
1518 ;; Prepare the final format string for the table.
1519 (cond
1520 ;; Longtable.
1521 ;; Others.
1522 (t (concat
1523 (format "@multitable %s\n%s@end multitable"
1524 columns
1525 contents))))))
1527 (defun org-texinfo-table--table.el-table (table contents info)
1528 "Returns nothing.
1530 Rather than return an invalid table, nothing is returned."
1531 'nil)
1533 ;;; Table Cell
1535 (defun org-texinfo-table-cell (table-cell contents info)
1536 "Transcode a TABLE-CELL element from Org to Texinfo.
1537 CONTENTS is the cell contents. INFO is a plist used as
1538 a communication channel."
1539 (concat (if (and contents
1540 org-texinfo-table-scientific-notation
1541 (string-match orgtbl-exp-regexp contents))
1542 ;; Use appropriate format string for scientific
1543 ;; notation.
1544 (format org-texinfo-table-scientific-notation
1545 (match-string 1 contents)
1546 (match-string 2 contents))
1547 contents)
1548 (when (org-export-get-next-element table-cell info) "\n@tab ")))
1550 ;;; Table Row
1552 (defun org-texinfo-table-row (table-row contents info)
1553 "Transcode a TABLE-ROW element from Org to Texinfo.
1554 CONTENTS is the contents of the row. INFO is a plist used as
1555 a communication channel."
1556 ;; Rules are ignored since table separators are deduced from
1557 ;; borders of the current row.
1558 (when (eq (org-element-property :type table-row) 'standard)
1559 (let ((rowgroup-tag
1560 (cond
1561 ;; Case 1: Belongs to second or subsequent rowgroup.
1562 ((not (= 1 (org-export-table-row-group table-row info)))
1563 "@item ")
1564 ;; Case 2: Row is from first rowgroup. Table has >=1 rowgroups.
1565 ((org-export-table-has-header-p
1566 (org-export-get-parent-table table-row) info)
1567 "@headitem ")
1568 ;; Case 3: Row is from first and only row group.
1569 (t "@item "))))
1570 (when (eq (org-element-property :type table-row) 'standard)
1571 (concat rowgroup-tag contents "\n")))))
1573 ;;; Target
1575 (defun org-texinfo-target (target contents info)
1576 "Transcode a TARGET object from Org to Texinfo.
1577 CONTENTS is nil. INFO is a plist holding contextual
1578 information."
1579 (format "@anchor{%s}"
1580 (org-export-solidify-link-text (org-element-property :value target))))
1582 ;;; Timestamp
1584 (defun org-texinfo-timestamp (timestamp contents info)
1585 "Transcode a TIMESTAMP object from Org to Texinfo.
1586 CONTENTS is nil. INFO is a plist holding contextual
1587 information."
1588 (let ((value (org-texinfo-plain-text
1589 (org-timestamp-translate timestamp) info)))
1590 (case (org-element-property :type timestamp)
1591 ((active active-range)
1592 (format org-texinfo-active-timestamp-format value))
1593 ((inactive inactive-range)
1594 (format org-texinfo-inactive-timestamp-format value))
1595 (t (format org-texinfo-diary-timestamp-format value)))))
1597 ;;; Verbatim
1599 (defun org-texinfo-verbatim (verbatim contents info)
1600 "Transcode a VERBATIM object from Org to Texinfo.
1601 CONTENTS is nil. INFO is a plist used as a communication
1602 channel."
1603 (org-texinfo--text-markup (org-element-property :value verbatim) 'verbatim))
1605 ;;; Verse Block
1607 (defun org-texinfo-verse-block (verse-block contents info)
1608 "Transcode a VERSE-BLOCK element from Org to Texinfo.
1609 CONTENTS is verse block contents. INFO is a plist holding
1610 contextual information."
1611 ;; In a verse environment, add a line break to each newline
1612 ;; character and change each white space at beginning of a line
1613 ;; into a space of 1 em. Also change each blank line with
1614 ;; a vertical space of 1 em.
1615 (progn
1616 (setq contents (replace-regexp-in-string
1617 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
1618 (replace-regexp-in-string
1619 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
1620 (while (string-match "^[ \t]+" contents)
1621 (let ((new-str (format "\\hspace*{%dem}"
1622 (length (match-string 0 contents)))))
1623 (setq contents (replace-match new-str nil t contents))))
1624 (format "\\begin{verse}\n%s\\end{verse}" contents)))
1627 ;;; Interactive functions
1629 (defun org-texinfo-export-to-texinfo
1630 (&optional async subtreep visible-only body-only ext-plist)
1631 "Export current buffer to a Texinfo file.
1633 If narrowing is active in the current buffer, only export its
1634 narrowed part.
1636 If a region is active, export that region.
1638 A non-nil optional argument ASYNC means the process should happen
1639 asynchronously. The resulting file should be accessible through
1640 the `org-export-stack' interface.
1642 When optional argument SUBTREEP is non-nil, export the sub-tree
1643 at point, extracting information from the headline properties
1644 first.
1646 When optional argument VISIBLE-ONLY is non-nil, don't export
1647 contents of hidden elements.
1649 When optional argument BODY-ONLY is non-nil, only write code
1650 between \"\\begin{document}\" and \"\\end{document}\".
1652 EXT-PLIST, when provided, is a property list with external
1653 parameters overriding Org default settings, but still inferior to
1654 file-local settings.
1656 Return output file's name."
1657 (interactive)
1658 (let ((outfile (org-export-output-file-name ".texi" subtreep)))
1659 (if async
1660 (org-export-async-start
1661 (lambda (f) (org-export-add-to-stack f 'texinfo))
1662 `(expand-file-name
1663 (org-export-to-file
1664 'texinfo ,outfile ,subtreep ,visible-only ,body-only
1665 ',ext-plist)))
1666 (org-export-to-file
1667 'texinfo outfile subtreep visible-only body-only ext-plist))))
1669 (defun org-texinfo-export-to-info
1670 (&optional async subtreep visible-only body-only ext-plist)
1671 "Export current buffer to Texinfo then process through to INFO.
1673 If narrowing is active in the current buffer, only export its
1674 narrowed part.
1676 If a region is active, export that region.
1678 A non-nil optional argument ASYNC means the process should happen
1679 asynchronously. The resulting file should be accessible through
1680 the `org-export-stack' interface.
1682 When optional argument SUBTREEP is non-nil, export the sub-tree
1683 at point, extracting information from the headline properties
1684 first.
1686 When optional argument VISIBLE-ONLY is non-nil, don't export
1687 contents of hidden elements.
1689 When optional argument BODY-ONLY is non-nil, only write code
1690 between \"\\begin{document}\" and \"\\end{document}\".
1692 EXT-PLIST, when provided, is a property list with external
1693 parameters overriding Org default settings, but still inferior to
1694 file-local settings.
1696 When optional argument PUB-DIR is set, use it as the publishing
1697 directory.
1699 Return INFO file's name."
1700 (interactive)
1701 (if async
1702 (let ((outfile (org-export-output-file-name ".texi" subtreep)))
1703 (org-export-async-start
1704 (lambda (f) (org-export-add-to-stack f 'texinfo))
1705 `(expand-file-name
1706 (org-texinfo-compile
1707 (org-export-to-file
1708 'texinfo ,outfile ,subtreep ,visible-only ,body-only
1709 ',ext-plist)))))
1710 (org-texinfo-compile
1711 (org-texinfo-export-to-texinfo
1712 nil subtreep visible-only body-only ext-plist))))
1714 (defun org-texinfo-compile (file)
1715 "Compile a texinfo file.
1717 FILE is the name of the file being compiled. Processing is
1718 done through the command specified in `org-texinfo-info-process'.
1720 Return INFO file name or an error if it couldn't be produced."
1721 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1722 (full-name (file-truename file))
1723 (out-dir (file-name-directory file))
1724 ;; Make sure `default-directory' is set to FILE directory,
1725 ;; not to whatever value the current buffer may have.
1726 (default-directory (file-name-directory full-name))
1727 errors)
1728 (message (format "Processing Texinfo file %s ..." file))
1729 (save-window-excursion
1730 (cond
1731 ;; A function is provided: Apply it.
1732 ((functionp org-texinfo-info-process)
1733 (funcall org-texinfo-info-process (shell-quote-argument file)))
1734 ;; A list is provided: Replace %b, %f and %o with appropriate
1735 ;; values in each command before applying it. Output is
1736 ;; redirected to "*Org INFO Texinfo Output*" buffer.
1737 ((consp org-texinfo-info-process)
1738 (let ((outbuf (get-buffer-create "*Org INFO Texinfo Output*")))
1739 (mapc
1740 (lambda (command)
1741 (shell-command
1742 (replace-regexp-in-string
1743 "%b" (shell-quote-argument base-name)
1744 (replace-regexp-in-string
1745 "%f" (shell-quote-argument full-name)
1746 (replace-regexp-in-string
1747 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1748 outbuf))
1749 org-texinfo-info-process)
1750 ;; Collect standard errors from output buffer.
1751 (setq errors (org-texinfo-collect-errors outbuf))))
1752 (t (error "No valid command to process to Info")))
1753 (let ((infofile (concat out-dir base-name ".info")))
1754 ;; Check for process failure. Provide collected errors if
1755 ;; possible.
1756 (if (not (file-exists-p infofile))
1757 (error (concat (format "INFO file %s wasn't produced" infofile)
1758 (when errors (concat ": " errors))))
1759 ;; Else remove log files, when specified, and signal end of
1760 ;; process to user, along with any error encountered.
1761 (message (concat "Process completed"
1762 (if (not errors) "."
1763 (concat " with errors: " errors)))))
1764 ;; Return output file name.
1765 infofile))))
1767 (defun org-texinfo-collect-errors (buffer)
1768 "Collect some kind of errors from \"makeinfo\" command output.
1770 BUFFER is the buffer containing output.
1772 Return collected error types as a string, or nil if there was
1773 none."
1774 (with-current-buffer buffer
1775 (save-excursion
1776 (goto-char (point-min))
1777 ;; Find final "makeinfo" run.
1778 (when t
1779 (let ((case-fold-search t)
1780 (errors ""))
1781 (when (save-excursion
1782 (re-search-forward "perhaps incorrect sectioning?" nil t))
1783 (setq errors (concat errors " [incorrect sectioning]")))
1784 (when (save-excursion
1785 (re-search-forward "missing close brace" nil t))
1786 (setq errors (concat errors " [syntax error]")))
1787 (when (save-excursion
1788 (re-search-forward "Unknown command" nil t))
1789 (setq errors (concat errors " [undefined @command]")))
1790 (when (save-excursion
1791 (re-search-forward "No matching @end" nil t))
1792 (setq errors (concat errors " [block incomplete]")))
1793 (when (save-excursion
1794 (re-search-forward "requires a sectioning" nil t))
1795 (setq errors (concat errors " [invalid section command]")))
1796 (when (save-excursion
1797 (re-search-forward "\\[unexpected\]" nil t))
1798 (setq errors (concat errors " [unexpected error]")))
1799 (when (save-excursion
1800 (re-search-forward "misplaced " nil t))
1801 (setq errors (concat errors " [syntax error]")))
1802 (and (org-string-nw-p errors) (org-trim errors)))))))
1805 (provide 'ox-texinfo)
1807 ;; Local variables:
1808 ;; generated-autoload-file: "org-loaddefs.el"
1809 ;; End:
1811 ;;; ox-texinfo.el ends here