ox-texinfo.el: Use :OPTIONAL_TITLE: instead of backend-specific
[org-mode.git] / lisp / ox-texinfo.el
blob44d466f03d66d1d10cc0e515495c8313d7f7bda7
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
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 ;; 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)))
135 ;;; User Configurable Variables
137 (defgroup org-export-texinfo nil
138 "Options for exporting Org mode files to Texinfo."
139 :tag "Org Export Texinfo"
140 :group 'org-export)
142 ;;; Preamble
144 (defcustom org-texinfo-filename nil
145 "Default filename for texinfo output."
146 :group 'org-export-texinfo
147 :type '(string :tag "Export Filename"))
149 (defcustom org-texinfo-default-class "info"
150 "The default Texinfo class."
151 :group 'org-export-texinfo
152 :type '(string :tag "Texinfo class"))
154 (defcustom org-texinfo-classes
155 '(("info"
156 "\\input texinfo @c -*- texinfo -*-"
157 ("@chapter %s" . "@unnumbered %s")
158 ("@section %s" . "@unnumberedsec %s")
159 ("@subsection %s" . "@unnumberedsubsec %s")
160 ("@subsubsection %s" . "@unnumberedsubsubsec %s")))
161 "Alist of Texinfo classes and associated header and structure.
162 If #+Texinfo_CLASS is set in the buffer, use its value and the
163 associated information. Here is the structure of each cell:
165 \(class-name
166 header-string
167 \(numbered-section . unnumbered-section\)
168 ...\)
170 The sectioning structure
171 ------------------------
173 The sectioning structure of the class is given by the elements
174 following the header string. For each sectioning level, a number
175 of strings is specified. A %s formatter is mandatory in each
176 section string and will be replaced by the title of the section.
178 Instead of a list of sectioning commands, you can also specify
179 a function name. That function will be called with two
180 parameters, the \(reduced) level of the headline, and a predicate
181 non-nil when the headline should be numbered. It must return
182 a format string in which the section title will be added."
183 :group 'org-export-texinfo
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 nil
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
228 listings.
230 If a node title is greater than this length, the description will
231 be placed after the end of the title."
232 :group 'org-export-texinfo
233 :type 'integer)
235 ;;; Footnotes
237 ;; Footnotes are inserted directly
239 ;;; Timestamps
241 (defcustom org-texinfo-active-timestamp-format "@emph{%s}"
242 "A printf format string to be applied to active timestamps."
243 :group 'org-export-texinfo
244 :type 'string)
246 (defcustom org-texinfo-inactive-timestamp-format "@emph{%s}"
247 "A printf format string to be applied to inactive timestamps."
248 :group 'org-export-texinfo
249 :type 'string)
251 (defcustom org-texinfo-diary-timestamp-format "@emph{%s}"
252 "A printf format string to be applied to diary timestamps."
253 :group 'org-export-texinfo
254 :type 'string)
256 ;;; Links
258 (defcustom org-texinfo-link-with-unknown-path-format "@indicateurl{%s}"
259 "Format string for links with unknown path type."
260 :group 'org-export-texinfo
261 :type 'string)
263 ;;; Tables
265 (defcustom org-texinfo-tables-verbatim nil
266 "When non-nil, tables are exported verbatim."
267 :group 'org-export-texinfo
268 :type 'boolean)
270 (defcustom org-texinfo-table-scientific-notation "%s\\,(%s)"
271 "Format string to display numbers in scientific notation.
272 The format should have \"%s\" twice, for mantissa and exponent
273 \(i.e. \"%s\\\\times10^{%s}\").
275 When nil, no transformation is made."
276 :group 'org-export-texinfo
277 :type '(choice
278 (string :tag "Format string")
279 (const :tag "No formatting")))
281 (defcustom org-texinfo-def-table-markup "@samp"
282 "Default setting for @table environments.")
284 ;;; Text markup
286 (defcustom org-texinfo-text-markup-alist '((bold . "@strong{%s}")
287 (code . code)
288 (italic . "@emph{%s}")
289 (verbatim . verb)
290 (comment . "@c %s"))
291 "Alist of Texinfo expressions to convert text markup.
293 The key must be a symbol among `bold', `italic' and `comment'.
294 The value is a formatting string to wrap fontified text with.
296 Value can also be set to the following symbols: `verb' and
297 `code'. For the former, Org will use \"@verb\" to
298 create a format string and select a delimiter character that
299 isn't in the string. For the latter, Org will use \"@code\"
300 to typeset and try to protect special characters.
302 If no association can be found for a given markup, text will be
303 returned as-is."
304 :group 'org-export-texinfo
305 :type 'alist
306 :options '(bold code italic verbatim comment))
308 ;;; Drawers
310 (defcustom org-texinfo-format-drawer-function nil
311 "Function called to format a drawer in Texinfo code.
313 The function must accept two parameters:
314 NAME the drawer name, like \"LOGBOOK\"
315 CONTENTS the contents of the drawer.
317 The function should return the string to be exported.
319 For example, the variable could be set to the following function
320 in order to mimic default behaviour:
322 \(defun org-texinfo-format-drawer-default \(name contents\)
323 \"Format a drawer element for Texinfo export.\"
324 contents\)"
325 :group 'org-export-texinfo
326 :type 'function)
328 ;;; Inlinetasks
330 (defcustom org-texinfo-format-inlinetask-function nil
331 "Function called to format an inlinetask in Texinfo code.
333 The function must accept six parameters:
334 TODO the todo keyword, as a string
335 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
336 PRIORITY the inlinetask priority, as a string
337 NAME the inlinetask name, as a string.
338 TAGS the inlinetask tags, as a list of strings.
339 CONTENTS the contents of the inlinetask, as a string.
341 The function should return the string to be exported.
343 For example, the variable could be set to the following function
344 in order to mimic default behaviour:
346 \(defun org-texinfo-format-inlinetask \(todo type priority name tags contents\)
347 \"Format an inline task element for Texinfo export.\"
348 \(let ((full-title
349 \(concat
350 \(when todo
351 \(format \"@strong{%s} \" todo))
352 \(when priority (format \"#%c \" priority))
353 title
354 \(when tags
355 \(format \":%s:\"
356 \(mapconcat 'identity tags \":\")))))
357 \(format (concat \"@center %s\n\n\"
358 \"%s\"
359 \"\n\"))
360 full-title contents))"
361 :group 'org-export-texinfo
362 :type 'function)
364 ;;; Src blocks
366 ;; Src Blocks are example blocks, except for LISP
368 ;;; Compilation
370 (defcustom org-texinfo-info-process
371 '("makeinfo %f")
372 "Commands to process a texinfo file to an INFO file.
373 This is list of strings, each of them will be given to the shell
374 as a command. %f in the command will be replaced by the full
375 file name, %b by the file base name \(i.e without extension) and
376 %o by the base directory of the file."
377 :group 'org-export-texinfo
378 :type '(repeat :tag "Shell command sequence"
379 (string :tag "Shell command")))
381 ;;; Constants
382 (defconst org-texinfo-max-toc-depth 4
383 "Maximum depth for creation of detailed menu listings. Beyond
384 this depth texinfo will not recognize the nodes and will cause
385 errors. Left as a constant in case this value ever changes.")
388 ;;; Internal Functions
390 (defun org-texinfo-filter-section-blank-lines (headline back-end info)
391 "Filter controlling number of blank lines after a section."
392 (let ((blanks (make-string 2 ?\n)))
393 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline)))
395 (defun org-texinfo--find-verb-separator (s)
396 "Return a character not used in string S.
397 This is used to choose a separator for constructs like \\verb."
398 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
399 (loop for c across ll
400 when (not (string-match (regexp-quote (char-to-string c)) s))
401 return (char-to-string c))))
403 (defun org-texinfo--make-option-string (options)
404 "Return a comma separated string of keywords and values.
405 OPTIONS is an alist where the key is the options keyword as
406 a string, and the value a list containing the keyword value, or
407 nil."
408 (mapconcat (lambda (pair)
409 (concat (first pair)
410 (when (> (length (second pair)) 0)
411 (concat "=" (second pair)))))
412 options
413 ","))
415 (defun org-texinfo--text-markup (text markup)
416 "Format TEXT depending on MARKUP text markup.
417 See `org-texinfo-text-markup-alist' for details."
418 (let ((fmt (cdr (assq markup org-texinfo-text-markup-alist))))
419 (cond
420 ;; No format string: Return raw text.
421 ((not fmt) text)
422 ((eq 'verb fmt)
423 (let ((separator (org-texinfo--find-verb-separator text)))
424 (concat "@verb{" separator text separator "}")))
425 ((eq 'code fmt)
426 (let ((start 0)
427 (rtn "")
428 char)
429 (while (string-match "[@{}]" text)
430 (setq char (match-string 0 text))
431 (if (> (match-beginning 0) 0)
432 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
433 (setq text (substring text (1+ (match-beginning 0))))
434 (setq char (concat "@" char)
435 rtn (concat rtn char)))
436 (setq text (concat rtn text)
437 fmt "@code{%s}")
438 (format fmt text)))
439 ;; Else use format string.
440 (t (format fmt text)))))
442 (defun org-texinfo--get-node (headline info)
443 "Return node entry associated to HEADLINE.
444 INFO is a plist used as a communication channel."
445 (let ((menu-title (org-export-get-optional-title headline info)))
446 (org-texinfo--sanitize-menu
447 (replace-regexp-in-string
448 "%" "%%"
449 (if menu-title (org-export-data menu-title info)
450 (org-texinfo--sanitize-headline
451 (org-element-property :title headline) info))))))
453 ;;; Headline sanitizing
455 (defun org-texinfo--sanitize-headline (headline info)
456 "Remove all formatting from the text of a headline for use in
457 node and menu listing."
458 (mapconcat 'identity
459 (org-texinfo--sanitize-headline-contents headline info) " "))
461 (defun org-texinfo--sanitize-headline-contents (headline info)
462 "Retrieve the content of the headline.
464 Any content that can contain further formatting is checked
465 recursively, to ensure that nested content is also properly
466 retrieved."
467 (loop for contents in headline append
468 (cond
469 ;; already a string
470 ((stringp contents)
471 (list (replace-regexp-in-string " $" "" contents)))
472 ;; Is exported as-is (value)
473 ((org-element-map contents '(verbatim code)
474 (lambda (value) (org-element-property :value value)) info))
475 ;; Has content and recurse into the content
476 ((org-element-contents contents)
477 (org-texinfo--sanitize-headline-contents
478 (org-element-contents contents) info)))))
480 ;;; Menu sanitizing
482 (defun org-texinfo--sanitize-menu (title)
483 "Remove invalid characters from TITLE for use in menus and
484 nodes.
486 Based on TEXINFO specifications, the following must be removed:
487 @ { } ( ) : . ,"
488 (replace-regexp-in-string "[@{}():,.]" "" title))
490 ;;; Content sanitizing
492 (defun org-texinfo--sanitize-content (text)
493 "Ensure characters are properly escaped when used in headlines or blocks.
495 Escape characters are: @ { }"
496 (replace-regexp-in-string "\\\([@{}]\\\)" "@\\1" text))
498 ;;; Menu creation
500 (defun org-texinfo--build-menu (tree level info &optional detailed)
501 "Create the @menu/@end menu information from TREE at headline
502 level LEVEL.
504 TREE contains the parse-tree to work with, either of the entire
505 document or of a specific parent headline. LEVEL indicates what
506 level of headlines to look at when generating the menu. INFO is
507 a plist containing contextual information.
509 Detailed determines whether to build a single level of menu, or
510 recurse into all children as well."
511 (let ((menu (org-texinfo--generate-menu-list tree level info))
512 output text-menu)
513 (cond
514 (detailed
515 ;; Looping is done within the menu generation.
516 (setq text-menu (org-texinfo--generate-detailed menu level info)))
518 (setq text-menu (org-texinfo--generate-menu-items menu info))))
519 (when text-menu
520 (setq output (org-texinfo--format-menu text-menu))
521 (mapconcat 'identity output "\n"))))
523 (defun org-texinfo--generate-detailed (menu level info)
524 "Generate a detailed listing of all subheadings within MENU starting at LEVEL.
526 MENU is the parse-tree to work with. LEVEL is the starting level
527 for the menu headlines and from which recursion occurs. INFO is
528 a plist containing contextual information."
529 (when level
530 (let ((max-depth (min org-texinfo-max-toc-depth
531 (plist-get info :headline-levels))))
532 (when (> max-depth level)
533 (loop for headline in menu append
534 (let* ((title (org-texinfo--menu-headlines headline info))
535 ;; Create list of menu entries for the next level
536 (sublist (org-texinfo--generate-menu-list
537 headline (1+ level) info))
538 ;; Generate the menu items for that level. If
539 ;; there are none omit that heading completely,
540 ;; otherwise join the title to it's related entries.
541 (submenu (if (org-texinfo--generate-menu-items sublist info)
542 (append (list title)
543 (org-texinfo--generate-menu-items sublist info))
544 'nil))
545 ;; Start the process over the next level down.
546 (recursion (org-texinfo--generate-detailed sublist (1+ level) info)))
547 (setq recursion (append submenu recursion))
548 recursion))))))
550 (defun org-texinfo--generate-menu-list (tree level info)
551 "Generate the list of headlines that are within a given level
552 of the tree for further formatting.
554 TREE is the parse-tree containing the headlines. LEVEL is the
555 headline level to generate a list of. INFO is a plist holding
556 contextual information."
557 (org-element-map tree 'headline
558 (lambda (head)
559 (and (= (org-export-get-relative-level head info) level)
560 ;; Do not take note of footnotes or copying headlines.
561 (not (org-element-property :COPYING head))
562 (not (org-element-property :footnote-section-p head))
563 ;; Collect headline.
564 head))
565 info))
567 (defun org-texinfo--generate-menu-items (items info)
568 "Generate a list of headline information from the listing ITEMS.
570 ITEMS is a list of the headlines to be converted into entries.
571 INFO is a plist containing contextual information.
573 Returns a list containing the following information from each
574 headline: length, title, description. This is used to format the
575 menu using `org-texinfo--format-menu'."
576 (loop for headline in items collect
577 (let* ((menu-title (org-texinfo--sanitize-menu
578 (org-export-data
579 (org-export-get-optional-title headline info)
580 info)))
581 (title (org-texinfo--sanitize-menu
582 (org-texinfo--sanitize-headline
583 (org-element-property :title headline) info)))
584 (descr (org-export-data
585 (org-element-property :DESCRIPTION headline)
586 info))
587 (menu-entry (if (string= "" menu-title) title menu-title))
588 (len (length menu-entry))
589 (output (list len menu-entry descr)))
590 output)))
592 (defun org-texinfo--menu-headlines (headline info)
593 "Retrieve the title from HEADLINE.
595 INFO is a plist holding contextual information.
597 Return the headline as a list of (length title description) with
598 length of -1 and nil description. This is used in
599 `org-texinfo--format-menu' to identify headlines as opposed to
600 entries."
601 (let ((title (org-export-data
602 (org-element-property :title headline) info)))
603 (list -1 title 'nil)))
605 (defun org-texinfo--format-menu (text-menu)
606 "Format the TEXT-MENU items to be properly printed in the menu.
608 Each entry in the menu should be provided as (length title
609 description).
611 Headlines in the detailed menu are given length -1 to ensure they
612 are never confused with other entries. They also have no
613 description.
615 Other menu items are output as:
616 Title:: description
618 With the spacing between :: and description based on the length
619 of the longest menu entry."
621 (let (output)
622 (setq output
623 (mapcar (lambda (name)
624 (let* ((title (nth 1 name))
625 (desc (nth 2 name))
626 (length (nth 0 name))
627 (column (max
628 length
629 org-texinfo-node-description-column))
630 (spacing (- column length)
632 (if (> length -1)
633 (concat "* " title ":: "
634 (make-string spacing ?\s)
635 (if desc
636 (concat desc)))
637 (concat "\n" title "\n"))))
638 text-menu))
639 output))
641 ;;; Template
643 (defun org-texinfo-template (contents info)
644 "Return complete document string after Texinfo conversion.
645 CONTENTS is the transcoded contents string. INFO is a plist
646 holding export options."
647 (let* ((title (org-export-data (plist-get info :title) info))
648 (info-filename (or (plist-get info :texinfo-filename)
649 (file-name-nondirectory
650 (org-export-output-file-name ".info"))))
651 (author (org-export-data (plist-get info :author) info))
652 (texinfo-header (plist-get info :texinfo-header))
653 (texinfo-post-header (plist-get info :texinfo-post-header))
654 (subtitle (plist-get info :subtitle))
655 (subauthor (plist-get info :subauthor))
656 (class (plist-get info :texinfo-class))
657 (header (nth 1 (assoc class org-texinfo-classes)))
658 (copying
659 (org-element-map (plist-get info :parse-tree) 'headline
660 (lambda (hl) (and (org-element-property :COPYING hl) hl)) info t))
661 (dircat (plist-get info :texinfo-dircat))
662 (dirtitle (plist-get info :texinfo-dirtitle))
663 (dirdesc (plist-get info :texinfo-dirdesc))
664 ;; Spacing to align description (column 32 - 3 for `* ' and
665 ;; `.' in text.
666 (dirspacing (- 29 (length dirtitle)))
667 (menu (org-texinfo-make-menu info 'main))
668 (detail-menu (org-texinfo-make-menu info 'detailed)))
669 (concat
670 ;; Header
671 header "\n"
672 "@c %**start of header\n"
673 ;; Filename and Title
674 "@setfilename " info-filename "\n"
675 "@settitle " title "\n"
676 "\n\n"
677 "@c Version and Contact Info\n"
678 "@set AUTHOR " author "\n"
680 ;; Additional Header Options set by `#+TEXINFO_HEADER
681 (if texinfo-header
682 (concat "\n"
683 texinfo-header
684 "\n"))
686 "@c %**end of header\n"
687 "@finalout\n"
688 "\n\n"
690 ;; Additional Header Options set by #+TEXINFO_POST_HEADER
691 (if texinfo-post-header
692 (concat "\n"
693 texinfo-post-header
694 "\n"))
696 ;; Copying
697 "@copying\n"
698 ;; Only export the content of the headline, do not need the
699 ;; initial headline.
700 (org-export-data (nth 2 copying) info)
701 "@end copying\n"
702 "\n\n"
704 ;; Info directory information
705 ;; Only supply if both title and category are provided
706 (if (and dircat dirtitle)
707 (concat "@dircategory " dircat "\n"
708 "@direntry\n"
709 "* " dirtitle "."
710 (make-string dirspacing ?\s)
711 dirdesc "\n"
712 "@end direntry\n"))
713 "\n\n"
715 ;; Title
716 "@titlepage\n"
717 "@title " title "\n\n"
718 (if subtitle
719 (concat "@subtitle " subtitle "\n"))
720 "@author " author "\n"
721 (if subauthor
722 (concat subauthor "\n"))
723 "\n"
724 "@c The following two commands start the copyright page.\n"
725 "@page\n"
726 "@vskip 0pt plus 1filll\n"
727 "@insertcopying\n"
728 "@end titlepage\n\n"
729 "@c Output the table of contents at the beginning.\n"
730 "@contents\n\n"
732 ;; Configure Top Node when not for Tex
733 "@ifnottex\n"
734 "@node Top\n"
735 "@top " title " Manual\n"
736 "@insertcopying\n"
737 "@end ifnottex\n\n"
739 ;; Do not output menus if they are empty
740 (if menu
741 ;; Menu
742 (concat "@menu\n"
743 menu
744 "\n\n"
745 ;; Detailed Menu
746 (if detail-menu
747 (concat "@detailmenu\n"
748 " --- The Detailed Node Listing ---\n"
749 detail-menu
750 "\n\n"
751 "@end detailmenu\n"))
752 "@end menu\n"))
753 "\n\n"
755 ;; Document's body.
756 contents
757 "\n"
758 ;; Creator.
759 (let ((creator-info (plist-get info :with-creator)))
760 (cond
761 ((not creator-info) "")
762 ((eq creator-info 'comment)
763 (format "@c %s\n" (plist-get info :creator)))
764 (t (concat (plist-get info :creator) "\n"))))
765 ;; Document end.
766 "\n@bye")))
770 ;;; Transcode Functions
772 ;;; Bold
774 (defun org-texinfo-bold (bold contents info)
775 "Transcode BOLD from Org to Texinfo.
776 CONTENTS is the text with bold markup. INFO is a plist holding
777 contextual information."
778 (org-texinfo--text-markup contents 'bold))
780 ;;; Center Block
782 (defun org-texinfo-center-block (center-block contents info)
783 "Transcode a CENTER-BLOCK element from Org to Texinfo.
784 CONTENTS holds the contents of the block. INFO is a plist used
785 as a communication channel."
786 contents)
788 ;;; Clock
790 (defun org-texinfo-clock (clock contents info)
791 "Transcode a CLOCK element from Org to Texinfo.
792 CONTENTS is nil. INFO is a plist holding contextual
793 information."
794 (concat
795 "@noindent"
796 (format "@strong{%s} " org-clock-string)
797 (format org-texinfo-inactive-timestamp-format
798 (concat (org-translate-time
799 (org-element-property :raw-value
800 (org-element-property :value clock)))
801 (let ((time (org-element-property :duration clock)))
802 (and time (format " (%s)" time)))))
803 "@*"))
805 ;;; Code
807 (defun org-texinfo-code (code contents info)
808 "Transcode a CODE object from Org to Texinfo.
809 CONTENTS is nil. INFO is a plist used as a communication
810 channel."
811 (org-texinfo--text-markup (org-element-property :value code) 'code))
813 ;;; Comment
815 (defun org-texinfo-comment (comment contents info)
816 "Transcode a COMMENT object from Org to Texinfo.
817 CONTENTS is the text in the comment. INFO is a plist holding
818 contextual information."
819 (org-texinfo--text-markup (org-element-property :value comment) 'comment))
821 ;;; Comment Block
823 (defun org-texinfo-comment-block (comment-block contents info)
824 "Transcode a COMMENT-BLOCK object from Org to Texinfo.
825 CONTENTS is the text within the block. INFO is a plist holding
826 contextual information."
827 (format "@ignore\n%s@end ignore" (org-element-property :value comment-block)))
829 ;;; Drawer
831 (defun org-texinfo-drawer (drawer contents info)
832 "Transcode a DRAWER element from Org to Texinfo.
833 CONTENTS holds the contents of the block. INFO is a plist
834 holding contextual information."
835 (let* ((name (org-element-property :drawer-name drawer))
836 (output (if (functionp org-texinfo-format-drawer-function)
837 (funcall org-texinfo-format-drawer-function
838 name contents)
839 ;; If there's no user defined function: simply
840 ;; display contents of the drawer.
841 contents)))
842 output))
844 ;;; Dynamic Block
846 (defun org-texinfo-dynamic-block (dynamic-block contents info)
847 "Transcode a DYNAMIC-BLOCK element from Org to Texinfo.
848 CONTENTS holds the contents of the block. INFO is a plist
849 holding contextual information. See `org-export-data'."
850 contents)
852 ;;; Entity
854 (defun org-texinfo-entity (entity contents info)
855 "Transcode an ENTITY object from Org to Texinfo.
856 CONTENTS are the definition itself. INFO is a plist holding
857 contextual information."
858 (let ((ent (org-element-property :latex entity)))
859 (if (org-element-property :latex-math-p entity) (format "@math{%s}" ent) ent)))
861 ;;; Example Block
863 (defun org-texinfo-example-block (example-block contents info)
864 "Transcode an EXAMPLE-BLOCK element from Org to Texinfo.
865 CONTENTS is nil. INFO is a plist holding contextual
866 information."
867 (format "@verbatim\n%s@end verbatim"
868 (org-export-format-code-default example-block info)))
870 ;;; Export Block
872 (defun org-texinfo-export-block (export-block contents info)
873 "Transcode a EXPORT-BLOCK element from Org to Texinfo.
874 CONTENTS is nil. INFO is a plist holding contextual information."
875 (when (string= (org-element-property :type export-block) "TEXINFO")
876 (org-remove-indentation (org-element-property :value export-block))))
878 ;;; Export Snippet
880 (defun org-texinfo-export-snippet (export-snippet contents info)
881 "Transcode a EXPORT-SNIPPET object from Org to Texinfo.
882 CONTENTS is nil. INFO is a plist holding contextual information."
883 (when (eq (org-export-snippet-backend export-snippet) 'texinfo)
884 (org-element-property :value export-snippet)))
886 ;;; Fixed Width
888 (defun org-texinfo-fixed-width (fixed-width contents info)
889 "Transcode a FIXED-WIDTH element from Org to Texinfo.
890 CONTENTS is nil. INFO is a plist holding contextual information."
891 (format "@example\n%s\n@end example"
892 (org-remove-indentation
893 (org-texinfo--sanitize-content
894 (org-element-property :value fixed-width)))))
896 ;;; Footnote Reference
899 (defun org-texinfo-footnote-reference (footnote contents info)
900 "Create a footnote reference for FOOTNOTE.
902 FOOTNOTE is the footnote to define. CONTENTS is nil. INFO is a
903 plist holding contextual information."
904 (let ((def (org-export-get-footnote-definition footnote info)))
905 (format "@footnote{%s}"
906 (org-trim (org-export-data def info)))))
908 ;;; Headline
910 (defun org-texinfo-headline (headline contents info)
911 "Transcode a HEADLINE element from Org to Texinfo.
912 CONTENTS holds the contents of the headline. INFO is a plist
913 holding contextual information."
914 (let* ((class (plist-get info :texinfo-class))
915 (level (org-export-get-relative-level headline info))
916 (numberedp (org-export-numbered-headline-p headline info))
917 (class-sectionning (assoc class org-texinfo-classes))
918 ;; Find the index type, if any
919 (index (org-element-property :INDEX headline))
920 ;; Check if it is an appendix
921 (appendix (org-element-property :APPENDIX headline))
922 ;; Retrieve headline text
923 (text (org-texinfo--sanitize-headline
924 (org-element-property :title headline) info))
925 ;; Create node info, to insert it before section formatting.
926 ;; Use custom menu title if present
927 (node (format "@node %s\n" (org-texinfo--get-node headline info)))
928 ;; Menus must be generated with first child, otherwise they
929 ;; will not nest properly
930 (menu (let* ((first (org-export-first-sibling-p headline info))
931 (parent (org-export-get-parent-headline headline))
932 (title (org-texinfo--sanitize-headline
933 (org-element-property :title parent) info))
934 heading listing
935 (tree (plist-get info :parse-tree)))
936 (if first
937 (org-element-map (plist-get info :parse-tree) 'headline
938 (lambda (ref)
939 (if (member title (org-element-property :title ref))
940 (push ref heading)))
941 info t))
942 (setq listing (org-texinfo--build-menu
943 (car heading) level info))
944 (if listing
945 (setq listing (replace-regexp-in-string
946 "%" "%%" listing)
947 listing (format
948 "\n@menu\n%s\n@end menu\n\n" listing))
949 'nil)))
950 ;; Section formatting will set two placeholders: one for the
951 ;; title and the other for the contents.
952 (section-fmt
953 (let ((sec (if (and (symbolp (nth 2 class-sectionning))
954 (fboundp (nth 2 class-sectionning)))
955 (funcall (nth 2 class-sectionning) level numberedp)
956 (nth (1+ level) class-sectionning))))
957 (cond
958 ;; No section available for that LEVEL.
959 ((not sec) nil)
960 ;; Section format directly returned by a function.
961 ((stringp sec) sec)
962 ;; (numbered-section . unnumbered-section)
963 ((not (consp (cdr sec)))
964 (cond
965 ;;If an index, always unnumbered
966 (index
967 (concat menu node (cdr sec) "\n%s"))
968 (appendix
969 (concat menu node (replace-regexp-in-string
970 "unnumbered"
971 "appendix"
972 (cdr sec)) "\n%s"))
973 ;; Otherwise number as needed.
975 (concat menu node
976 (funcall
977 (if numberedp #'car #'cdr) sec) "\n%s")))))))
978 (todo
979 (and (plist-get info :with-todo-keywords)
980 (let ((todo (org-element-property :todo-keyword headline)))
981 (and todo (org-export-data todo info)))))
982 (todo-type (and todo (org-element-property :todo-type headline)))
983 (tags (and (plist-get info :with-tags)
984 (org-export-get-tags headline info)))
985 (priority (and (plist-get info :with-priority)
986 (org-element-property :priority headline)))
987 ;; Create the headline text along with a no-tag version. The
988 ;; latter is required to remove tags from table of contents.
989 (full-text (org-texinfo--sanitize-content
990 (if (functionp org-texinfo-format-headline-function)
991 ;; User-defined formatting function.
992 (funcall org-texinfo-format-headline-function
993 todo todo-type priority text tags)
994 ;; Default formatting.
995 (concat
996 (when todo
997 (format "@strong{%s} " todo))
998 (when priority (format "@emph{#%s} " priority))
999 text
1000 (when tags
1001 (format ":%s:"
1002 (mapconcat 'identity tags ":")))))))
1003 (full-text-no-tag
1004 (org-texinfo--sanitize-content
1005 (if (functionp org-texinfo-format-headline-function)
1006 ;; User-defined formatting function.
1007 (funcall org-texinfo-format-headline-function
1008 todo todo-type priority text nil)
1009 ;; Default formatting.
1010 (concat
1011 (when todo (format "@strong{%s} " todo))
1012 (when priority (format "@emph{#%c} " priority))
1013 text))))
1014 (pre-blanks
1015 (make-string (org-element-property :pre-blank headline) 10)))
1016 (cond
1017 ;; Case 1: This is a footnote section: ignore it.
1018 ((org-element-property :footnote-section-p headline) nil)
1019 ;; Case 2: This is the `copying' section: ignore it
1020 ;; This is used elsewhere.
1021 ((org-element-property :COPYING headline) nil)
1022 ;; Case 3: An index. If it matches one of the known indexes,
1023 ;; print it as such following the contents, otherwise
1024 ;; print the contents and leave the index up to the user.
1025 (index
1026 (format
1027 section-fmt full-text
1028 (concat pre-blanks contents "\n"
1029 (if (member index '("cp" "fn" "ky" "pg" "tp" "vr"))
1030 (concat "@printindex " index)))))
1031 ;; Case 4: This is a deep sub-tree: export it as a list item.
1032 ;; Also export as items headlines for which no section
1033 ;; format has been found.
1034 ((or (not section-fmt) (org-export-low-level-p headline info))
1035 ;; Build the real contents of the sub-tree.
1036 (let ((low-level-body
1037 (concat
1038 ;; If the headline is the first sibling, start a list.
1039 (when (org-export-first-sibling-p headline info)
1040 (format "@%s\n" (if numberedp 'enumerate 'itemize)))
1041 ;; Itemize headline
1042 "@item\n" full-text "\n" pre-blanks contents)))
1043 ;; If headline is not the last sibling simply return
1044 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1045 ;; blank line.
1046 (if (not (org-export-last-sibling-p headline info)) low-level-body
1047 (replace-regexp-in-string
1048 "[ \t\n]*\\'"
1049 (format "\n@end %s" (if numberedp 'enumerate 'itemize))
1050 low-level-body))))
1051 ;; Case 5: Standard headline. Export it as a section.
1053 (cond
1054 ((not (and tags (eq (plist-get info :with-tags) 'not-in-toc)))
1055 ;; Regular section. Use specified format string.
1056 (format (replace-regexp-in-string "%]" "%%]" section-fmt) full-text
1057 (concat pre-blanks contents)))
1058 ((string-match "\\`@\\(.*?\\){" section-fmt)
1059 ;; If tags should be removed from table of contents, insert
1060 ;; title without tags as an alternative heading in sectioning
1061 ;; command.
1062 (format (replace-match (concat (match-string 1 section-fmt) "[%s]")
1063 nil nil section-fmt 1)
1064 ;; Replace square brackets with parenthesis since
1065 ;; square brackets are not supported in optional
1066 ;; arguments.
1067 (replace-regexp-in-string
1068 "\\[" "("
1069 (replace-regexp-in-string
1070 "\\]" ")"
1071 full-text-no-tag))
1072 full-text
1073 (concat pre-blanks contents)))
1075 ;; Impossible to add an alternative heading. Fallback to
1076 ;; regular sectioning format string.
1077 (format (replace-regexp-in-string "%]" "%%]" section-fmt) full-text
1078 (concat pre-blanks contents))))))))
1080 ;;; Inline Src Block
1082 (defun org-texinfo-inline-src-block (inline-src-block contents info)
1083 "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
1084 CONTENTS holds the contents of the item. INFO is a plist holding
1085 contextual information."
1086 (let* ((code (org-element-property :value inline-src-block))
1087 (separator (org-texinfo--find-verb-separator code)))
1088 (concat "@verb{" separator code separator "}")))
1090 ;;; Inlinetask
1092 (defun org-texinfo-inlinetask (inlinetask contents info)
1093 "Transcode an INLINETASK element from Org to Texinfo.
1094 CONTENTS holds the contents of the block. INFO is a plist
1095 holding contextual information."
1096 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1097 (todo (and (plist-get info :with-todo-keywords)
1098 (let ((todo (org-element-property :todo-keyword inlinetask)))
1099 (and todo (org-export-data todo info)))))
1100 (todo-type (org-element-property :todo-type inlinetask))
1101 (tags (and (plist-get info :with-tags)
1102 (org-export-get-tags inlinetask info)))
1103 (priority (and (plist-get info :with-priority)
1104 (org-element-property :priority inlinetask))))
1105 ;; If `org-texinfo-format-inlinetask-function' is provided, call it
1106 ;; with appropriate arguments.
1107 (if (functionp org-texinfo-format-inlinetask-function)
1108 (funcall org-texinfo-format-inlinetask-function
1109 todo todo-type priority title tags contents)
1110 ;; Otherwise, use a default template.
1111 (let ((full-title
1112 (concat
1113 (when todo (format "@strong{%s} " todo))
1114 (when priority (format "#%c " priority))
1115 title
1116 (when tags (format ":%s:"
1117 (mapconcat 'identity tags ":"))))))
1118 (format (concat "@center %s\n\n"
1119 "%s"
1120 "\n")
1121 full-title contents)))))
1123 ;;; Italic
1125 (defun org-texinfo-italic (italic contents info)
1126 "Transcode ITALIC from Org to Texinfo.
1127 CONTENTS is the text with italic markup. INFO is a plist holding
1128 contextual information."
1129 (org-texinfo--text-markup contents 'italic))
1131 ;;; Item
1133 (defun org-texinfo-item (item contents info)
1134 "Transcode an ITEM element from Org to Texinfo.
1135 CONTENTS holds the contents of the item. INFO is a plist holding
1136 contextual information."
1137 (let* ((tag (org-element-property :tag item))
1138 (desc (org-export-data tag info)))
1139 (concat "\n@item " (if tag desc) "\n"
1140 (org-trim contents) "\n")))
1142 ;;; Keyword
1144 (defun org-texinfo-keyword (keyword contents info)
1145 "Transcode a KEYWORD element from Org to Texinfo.
1146 CONTENTS is nil. INFO is a plist holding contextual information."
1147 (let ((key (org-element-property :key keyword))
1148 (value (org-element-property :value keyword)))
1149 (cond
1150 ((string= key "TEXINFO") value)
1151 ((string= key "CINDEX") (format "@cindex %s" value))
1152 ((string= key "FINDEX") (format "@findex %s" value))
1153 ((string= key "KINDEX") (format "@kindex %s" value))
1154 ((string= key "PINDEX") (format "@pindex %s" value))
1155 ((string= key "TINDEX") (format "@tindex %s" value))
1156 ((string= key "VINDEX") (format "@vindex %s" value)))))
1158 ;;; Line Break
1160 (defun org-texinfo-line-break (line-break contents info)
1161 "Transcode a LINE-BREAK object from Org to Texinfo.
1162 CONTENTS is nil. INFO is a plist holding contextual information."
1163 "@*\n")
1165 ;;; Link
1167 (defun org-texinfo-link (link desc info)
1168 "Transcode a LINK object from Org to Texinfo.
1170 DESC is the description part of the link, or the empty string.
1171 INFO is a plist holding contextual information. See
1172 `org-export-data'."
1173 (let* ((type (org-element-property :type link))
1174 (raw-path (org-element-property :path link))
1175 ;; Ensure DESC really exists, or set it to nil.
1176 (desc (and (not (string= desc "")) desc))
1177 (path (cond
1178 ((member type '("http" "https" "ftp"))
1179 (concat type ":" raw-path))
1180 ((string= type "file")
1181 (if (file-name-absolute-p raw-path)
1182 (concat "file://" (expand-file-name raw-path))
1183 (concat "file://" raw-path)))
1184 (t raw-path)))
1185 (email (if (string= type "mailto")
1186 (let ((text (replace-regexp-in-string
1187 "@" "@@" raw-path)))
1188 (concat text (if desc (concat "," desc))))))
1189 protocol)
1190 (cond
1191 ;; Links pointing to a headline: Find destination and build
1192 ;; appropriate referencing command.
1193 ((member type '("custom-id" "id"))
1194 (let ((destination (org-export-resolve-id-link link info)))
1195 (case (org-element-type destination)
1196 ;; Id link points to an external file.
1197 (plain-text
1198 (if desc (format "@uref{file://%s,%s}" destination desc)
1199 (format "@uref{file://%s}" destination)))
1200 ;; LINK points to a headline. Use the headline as the NODE target
1201 (headline
1202 (format "@ref{%s,%s}"
1203 (org-texinfo--get-node destination info)
1204 (or desc "")))
1205 (otherwise
1206 (let ((path (org-export-solidify-link-text path)))
1207 (if (not desc) (format "@ref{%s}" path)
1208 (format "@ref{%s,,%s}" path desc)))))))
1209 ((member type '("info"))
1210 (let* ((info-path (split-string path "[:#]"))
1211 (info-manual (car info-path))
1212 (info-node (or (cadr info-path) "top"))
1213 (title (or desc "")))
1214 (format "@ref{%s,%s,,%s,}" info-node title info-manual)))
1215 ((member type '("fuzzy"))
1216 (let ((destination (org-export-resolve-fuzzy-link link info)))
1217 (case (org-element-type destination)
1218 ;; Id link points to an external file.
1219 (plain-text
1220 (if desc (format "@uref{file://%s,%s}" destination desc)
1221 (format "@uref{file://%s}" destination)))
1222 ;; LINK points to a headline. Use the headline as the NODE target
1223 (headline
1224 (format "@ref{%s,%s}"
1225 (org-texinfo--get-node destination info)
1226 (or desc "")))
1227 (otherwise
1228 (let ((path (org-export-solidify-link-text path)))
1229 (if (not desc) (format "@ref{%s}" path)
1230 (format "@ref{%s,,%s}" path desc)))))))
1231 ;; Special case for email addresses
1232 (email
1233 (format "@email{%s}" email))
1234 ;; External link with a description part.
1235 ((and path desc) (format "@uref{%s,%s}" path desc))
1236 ;; External link without a description part.
1237 (path (format "@uref{%s}" path))
1238 ;; No path, only description. Try to do something useful.
1239 (t (format org-texinfo-link-with-unknown-path-format desc)))))
1242 ;;; Menu
1244 (defun org-texinfo-make-menu (info level)
1245 "Create the menu for inclusion in the texifo document.
1247 INFO is the parsed buffer that contains the headlines. LEVEL
1248 determines whether to make the main menu, or the detailed menu.
1250 This is only used for generating the primary menu. In-Node menus
1251 are generated directly."
1252 (let ((parse (plist-get info :parse-tree)))
1253 (cond
1254 ;; Generate the main menu
1255 ((eq level 'main) (org-texinfo--build-menu parse 1 info))
1256 ;; Generate the detailed (recursive) menu
1257 ((eq level 'detailed)
1258 ;; Requires recursion
1259 ;;(org-texinfo--build-detailed-menu parse top info)
1260 (org-texinfo--build-menu parse 1 info 'detailed)))))
1262 ;;; Paragraph
1264 (defun org-texinfo-paragraph (paragraph contents info)
1265 "Transcode a PARAGRAPH element from Org to Texinfo.
1266 CONTENTS is the contents of the paragraph, as a string. INFO is
1267 the plist used as a communication channel."
1268 contents)
1270 ;;; Plain List
1272 (defun org-texinfo-plain-list (plain-list contents info)
1273 "Transcode a PLAIN-LIST element from Org to Texinfo.
1274 CONTENTS is the contents of the list. INFO is a plist holding
1275 contextual information."
1276 (let* ((attr (org-export-read-attribute :attr_texinfo plain-list))
1277 (indic (or (plist-get attr :indic)
1278 org-texinfo-def-table-markup))
1279 (type (org-element-property :type plain-list))
1280 (table-type (or (plist-get attr :table-type)
1281 "table"))
1282 ;; Ensure valid texinfo table type.
1283 (table-type (if (memq table-type '("table" "ftable" "vtable"))
1284 table-type
1285 "table"))
1286 (list-type (cond
1287 ((eq type 'ordered) "enumerate")
1288 ((eq type 'unordered) "itemize")
1289 ((eq type 'descriptive) table-type))))
1290 (format "@%s%s\n@end %s"
1291 (if (eq type 'descriptive)
1292 (concat list-type " " indic)
1293 list-type)
1294 contents
1295 list-type)))
1297 ;;; Plain Text
1299 (defun org-texinfo-plain-text (text info)
1300 "Transcode a TEXT string from Org to Texinfo.
1301 TEXT is the string to transcode. INFO is a plist holding
1302 contextual information."
1303 ;; First protect @, { and }.
1304 (let ((output (org-texinfo--sanitize-content text)))
1305 ;; Activate smart quotes. Be sure to provide original TEXT string
1306 ;; since OUTPUT may have been modified.
1307 (when (plist-get info :with-smart-quotes)
1308 (setq output
1309 (org-export-activate-smart-quotes output :texinfo info text)))
1310 ;; LaTeX into @LaTeX{} and TeX into @TeX{}
1311 (let ((case-fold-search nil)
1312 (start 0))
1313 (while (string-match "\\(\\(?:La\\)?TeX\\)" output start)
1314 (setq output (replace-match
1315 (format "@%s{}" (match-string 1 output)) nil t output)
1316 start (match-end 0))))
1317 ;; Convert special strings.
1318 (when (plist-get info :with-special-strings)
1319 (while (string-match (regexp-quote "...") output)
1320 (setq output (replace-match "@dots{}" nil t output))))
1321 ;; Handle break preservation if required.
1322 (when (plist-get info :preserve-breaks)
1323 (setq output (replace-regexp-in-string
1324 "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" output)))
1325 ;; Return value.
1326 output))
1328 ;;; Planning
1330 (defun org-texinfo-planning (planning contents info)
1331 "Transcode a PLANNING element from Org to Texinfo.
1332 CONTENTS is nil. INFO is a plist holding contextual
1333 information."
1334 (concat
1335 "@noindent"
1336 (mapconcat
1337 'identity
1338 (delq nil
1339 (list
1340 (let ((closed (org-element-property :closed planning)))
1341 (when closed
1342 (concat
1343 (format "@strong{%s} " org-closed-string)
1344 (format org-texinfo-inactive-timestamp-format
1345 (org-translate-time
1346 (org-element-property :raw-value closed))))))
1347 (let ((deadline (org-element-property :deadline planning)))
1348 (when deadline
1349 (concat
1350 (format "@strong{%s} " org-deadline-string)
1351 (format org-texinfo-active-timestamp-format
1352 (org-translate-time
1353 (org-element-property :raw-value deadline))))))
1354 (let ((scheduled (org-element-property :scheduled planning)))
1355 (when scheduled
1356 (concat
1357 (format "@strong{%s} " org-scheduled-string)
1358 (format org-texinfo-active-timestamp-format
1359 (org-translate-time
1360 (org-element-property :raw-value scheduled))))))))
1361 " ")
1362 "@*"))
1364 ;;; Property Drawer
1366 (defun org-texinfo-property-drawer (property-drawer contents info)
1367 "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
1368 CONTENTS is nil. INFO is a plist holding contextual
1369 information."
1370 ;; The property drawer isn't exported but we want separating blank
1371 ;; lines nonetheless.
1374 ;;; Quote Block
1376 (defun org-texinfo-quote-block (quote-block contents info)
1377 "Transcode a QUOTE-BLOCK element from Org to Texinfo.
1378 CONTENTS holds the contents of the block. INFO is a plist
1379 holding contextual information."
1380 (let* ((title (org-element-property :name quote-block))
1381 (start-quote (concat "@quotation"
1382 (if title
1383 (format " %s" title)))))
1384 (format "%s\n%s@end quotation" start-quote contents)))
1386 ;;; Quote Section
1388 (defun org-texinfo-quote-section (quote-section contents info)
1389 "Transcode a QUOTE-SECTION element from Org to Texinfo.
1390 CONTENTS is nil. INFO is a plist holding contextual information."
1391 (let ((value (org-remove-indentation
1392 (org-element-property :value quote-section))))
1393 (when value (format "@verbatim\n%s@end verbatim" value))))
1395 ;;; Radio Target
1397 (defun org-texinfo-radio-target (radio-target text info)
1398 "Transcode a RADIO-TARGET object from Org to Texinfo.
1399 TEXT is the text of the target. INFO is a plist holding
1400 contextual information."
1401 (format "@anchor{%s}%s"
1402 (org-export-solidify-link-text
1403 (org-element-property :value radio-target))
1404 text))
1406 ;;; Section
1408 (defun org-texinfo-section (section contents info)
1409 "Transcode a SECTION element from Org to Texinfo.
1410 CONTENTS holds the contents of the section. INFO is a plist
1411 holding contextual information."
1412 contents)
1414 ;;; Special Block
1416 (defun org-texinfo-special-block (special-block contents info)
1417 "Transcode a SPECIAL-BLOCK element from Org to Texinfo.
1418 CONTENTS holds the contents of the block. INFO is a plist used
1419 as a communication channel."
1420 contents)
1422 ;;; Src Block
1424 (defun org-texinfo-src-block (src-block contents info)
1425 "Transcode a SRC-BLOCK element from Org to Texinfo.
1426 CONTENTS holds the contents of the item. INFO is a plist holding
1427 contextual information."
1428 (let* ((lang (org-element-property :language src-block))
1429 (lisp-p (string-match-p "lisp" lang)))
1430 (cond
1431 ;; Case 1. Lisp Block
1432 (lisp-p
1433 (format "@lisp\n%s@end lisp"
1434 (org-export-format-code-default src-block info)))
1435 ;; Case 2. Other blocks
1437 (format "@example\n%s@end example"
1438 (org-export-format-code-default src-block info))))))
1440 ;;; Statistics Cookie
1442 (defun org-texinfo-statistics-cookie (statistics-cookie contents info)
1443 "Transcode a STATISTICS-COOKIE object from Org to Texinfo.
1444 CONTENTS is nil. INFO is a plist holding contextual information."
1445 (org-element-property :value statistics-cookie))
1447 ;;; Subscript
1449 (defun org-texinfo-subscript (subscript contents info)
1450 "Transcode a SUBSCRIPT object from Org to Texinfo.
1451 CONTENTS is the contents of the object. INFO is a plist holding
1452 contextual information."
1453 (format "@math{_%s}" contents))
1455 ;;; Superscript
1457 (defun org-texinfo-superscript (superscript contents info)
1458 "Transcode a SUPERSCRIPT object from Org to Texinfo.
1459 CONTENTS is the contents of the object. INFO is a plist holding
1460 contextual information."
1461 (format "@math{^%s}" contents))
1463 ;;; Table
1465 ;; `org-texinfo-table' is the entry point for table transcoding. It
1466 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
1467 ;; delegates the job to either `org-texinfo-table--table.el-table' or
1468 ;; `org-texinfo-table--org-table' functions, depending of the type of
1469 ;; the table.
1471 ;; `org-texinfo-table--align-string' is a subroutine used to build
1472 ;; alignment string for Org tables.
1474 (defun org-texinfo-table (table contents info)
1475 "Transcode a TABLE element from Org to Texinfo.
1476 CONTENTS is the contents of the table. INFO is a plist holding
1477 contextual information."
1478 (cond
1479 ;; Case 1: verbatim table.
1480 ((or org-texinfo-tables-verbatim
1481 (let ((attr (mapconcat 'identity
1482 (org-element-property :attr_latex table)
1483 " ")))
1484 (and attr (string-match "\\<verbatim\\>" attr))))
1485 (format "@verbatim \n%s\n@end verbatim"
1486 ;; Re-create table, without affiliated keywords.
1487 (org-trim
1488 (org-element-interpret-data
1489 `(table nil ,@(org-element-contents table))))))
1490 ;; Case 2: table.el table. Convert it using appropriate tools.
1491 ((eq (org-element-property :type table) 'table.el)
1492 (org-texinfo-table--table.el-table table contents info))
1493 ;; Case 3: Standard table.
1494 (t (org-texinfo-table--org-table table contents info))))
1496 (defun org-texinfo-table-column-widths (table info)
1497 "Determine the largest table cell in each column to process alignment.
1499 TABLE is the table element to transcode. INFO is a plist used as
1500 a communication channel."
1501 (let* ((rows (org-element-map table 'table-row 'identity info))
1502 (collected (loop for row in rows collect
1503 (org-element-map row 'table-cell 'identity info)))
1504 (number-cells (length (car collected)))
1505 cells counts)
1506 (loop for row in collected do
1507 (push (mapcar (lambda (ref)
1508 (let* ((start (org-element-property :contents-begin ref))
1509 (end (org-element-property :contents-end ref))
1510 (length (- end start)))
1511 length)) row) cells))
1512 (setq cells (org-remove-if 'null cells))
1513 (push (loop for count from 0 to (- number-cells 1) collect
1514 (loop for item in cells collect
1515 (nth count item))) counts)
1516 (mapconcat (lambda (size)
1517 (make-string size ?a)) (mapcar (lambda (ref)
1518 (apply 'max `,@ref)) (car counts))
1519 "} {")))
1521 (defun org-texinfo-table--org-table (table contents info)
1522 "Return appropriate Texinfo code for an Org table.
1524 TABLE is the table type element to transcode. CONTENTS is its
1525 contents, as a string. INFO is a plist used as a communication
1526 channel.
1528 This function assumes TABLE has `org' as its `:type' attribute."
1529 (let* ((attr (org-export-read-attribute :attr_texinfo table))
1530 (col-width (plist-get attr :columns))
1531 (columns (if col-width
1532 (format "@columnfractions %s"
1533 col-width)
1534 (format "{%s}"
1535 (org-texinfo-table-column-widths
1536 table info)))))
1537 ;; Prepare the final format string for the table.
1538 (cond
1539 ;; Longtable.
1540 ;; Others.
1541 (t (concat
1542 (format "@multitable %s\n%s@end multitable"
1543 columns
1544 contents))))))
1546 (defun org-texinfo-table--table.el-table (table contents info)
1547 "Returns nothing.
1549 Rather than return an invalid table, nothing is returned."
1550 'nil)
1552 ;;; Table Cell
1554 (defun org-texinfo-table-cell (table-cell contents info)
1555 "Transcode a TABLE-CELL element from Org to Texinfo.
1556 CONTENTS is the cell contents. INFO is a plist used as
1557 a communication channel."
1558 (concat (if (and contents
1559 org-texinfo-table-scientific-notation
1560 (string-match orgtbl-exp-regexp contents))
1561 ;; Use appropriate format string for scientific
1562 ;; notation.
1563 (format org-texinfo-table-scientific-notation
1564 (match-string 1 contents)
1565 (match-string 2 contents))
1566 contents)
1567 (when (org-export-get-next-element table-cell info) "\n@tab ")))
1569 ;;; Table Row
1571 (defun org-texinfo-table-row (table-row contents info)
1572 "Transcode a TABLE-ROW element from Org to Texinfo.
1573 CONTENTS is the contents of the row. INFO is a plist used as
1574 a communication channel."
1575 ;; Rules are ignored since table separators are deduced from
1576 ;; borders of the current row.
1577 (when (eq (org-element-property :type table-row) 'standard)
1578 (let ((rowgroup-tag
1579 (cond
1580 ;; Case 1: Belongs to second or subsequent rowgroup.
1581 ((not (= 1 (org-export-table-row-group table-row info)))
1582 "@item ")
1583 ;; Case 2: Row is from first rowgroup. Table has >=1 rowgroups.
1584 ((org-export-table-has-header-p
1585 (org-export-get-parent-table table-row) info)
1586 "@headitem ")
1587 ;; Case 3: Row is from first and only row group.
1588 (t "@item "))))
1589 (when (eq (org-element-property :type table-row) 'standard)
1590 (concat rowgroup-tag contents "\n")))))
1592 ;;; Target
1594 (defun org-texinfo-target (target contents info)
1595 "Transcode a TARGET object from Org to Texinfo.
1596 CONTENTS is nil. INFO is a plist holding contextual
1597 information."
1598 (format "@anchor{%s}"
1599 (org-export-solidify-link-text (org-element-property :value target))))
1601 ;;; Timestamp
1603 (defun org-texinfo-timestamp (timestamp contents info)
1604 "Transcode a TIMESTAMP object from Org to Texinfo.
1605 CONTENTS is nil. INFO is a plist holding contextual
1606 information."
1607 (let ((value (org-texinfo-plain-text
1608 (org-timestamp-translate timestamp) info)))
1609 (case (org-element-property :type timestamp)
1610 ((active active-range)
1611 (format org-texinfo-active-timestamp-format value))
1612 ((inactive inactive-range)
1613 (format org-texinfo-inactive-timestamp-format value))
1614 (t (format org-texinfo-diary-timestamp-format value)))))
1616 ;;; Verbatim
1618 (defun org-texinfo-verbatim (verbatim contents info)
1619 "Transcode a VERBATIM object from Org to Texinfo.
1620 CONTENTS is nil. INFO is a plist used as a communication
1621 channel."
1622 (org-texinfo--text-markup (org-element-property :value verbatim) 'verbatim))
1624 ;;; Verse Block
1626 (defun org-texinfo-verse-block (verse-block contents info)
1627 "Transcode a VERSE-BLOCK element from Org to Texinfo.
1628 CONTENTS is verse block contents. INFO is a plist holding
1629 contextual information."
1630 ;; In a verse environment, add a line break to each newline
1631 ;; character and change each white space at beginning of a line
1632 ;; into a space of 1 em. Also change each blank line with
1633 ;; a vertical space of 1 em.
1634 (progn
1635 (setq contents (replace-regexp-in-string
1636 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
1637 (replace-regexp-in-string
1638 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
1639 (while (string-match "^[ \t]+" contents)
1640 (let ((new-str (format "\\hspace*{%dem}"
1641 (length (match-string 0 contents)))))
1642 (setq contents (replace-match new-str nil t contents))))
1643 (format "\\begin{verse}\n%s\\end{verse}" contents)))
1646 ;;; Interactive functions
1648 (defun org-texinfo-export-to-texinfo
1649 (&optional async subtreep visible-only body-only ext-plist)
1650 "Export current buffer to a Texinfo file.
1652 If narrowing is active in the current buffer, only export its
1653 narrowed part.
1655 If a region is active, export that region.
1657 A non-nil optional argument ASYNC means the process should happen
1658 asynchronously. The resulting file should be accessible through
1659 the `org-export-stack' interface.
1661 When optional argument SUBTREEP is non-nil, export the sub-tree
1662 at point, extracting information from the headline properties
1663 first.
1665 When optional argument VISIBLE-ONLY is non-nil, don't export
1666 contents of hidden elements.
1668 When optional argument BODY-ONLY is non-nil, only write code
1669 between \"\\begin{document}\" and \"\\end{document}\".
1671 EXT-PLIST, when provided, is a property list with external
1672 parameters overriding Org default settings, but still inferior to
1673 file-local settings.
1675 Return output file's name."
1676 (interactive)
1677 (let ((outfile (org-export-output-file-name ".texi" subtreep)))
1678 (if async
1679 (org-export-async-start
1680 (lambda (f) (org-export-add-to-stack f 'texinfo))
1681 `(expand-file-name
1682 (org-export-to-file
1683 'texinfo ,outfile ,subtreep ,visible-only ,body-only
1684 ',ext-plist)))
1685 (org-export-to-file
1686 'texinfo outfile subtreep visible-only body-only ext-plist))))
1688 (defun org-texinfo-export-to-info
1689 (&optional async subtreep visible-only body-only ext-plist)
1690 "Export current buffer to Texinfo then process through to INFO.
1692 If narrowing is active in the current buffer, only export its
1693 narrowed part.
1695 If a region is active, export that region.
1697 A non-nil optional argument ASYNC means the process should happen
1698 asynchronously. The resulting file should be accessible through
1699 the `org-export-stack' interface.
1701 When optional argument SUBTREEP is non-nil, export the sub-tree
1702 at point, extracting information from the headline properties
1703 first.
1705 When optional argument VISIBLE-ONLY is non-nil, don't export
1706 contents of hidden elements.
1708 When optional argument BODY-ONLY is non-nil, only write code
1709 between \"\\begin{document}\" and \"\\end{document}\".
1711 EXT-PLIST, when provided, is a property list with external
1712 parameters overriding Org default settings, but still inferior to
1713 file-local settings.
1715 When optional argument PUB-DIR is set, use it as the publishing
1716 directory.
1718 Return INFO file's name."
1719 (interactive)
1720 (if async
1721 (let ((outfile (org-export-output-file-name ".texi" subtreep)))
1722 (org-export-async-start
1723 (lambda (f) (org-export-add-to-stack f 'texinfo))
1724 `(expand-file-name
1725 (org-texinfo-compile
1726 (org-export-to-file
1727 'texinfo ,outfile ,subtreep ,visible-only ,body-only
1728 ',ext-plist)))))
1729 (org-texinfo-compile
1730 (org-texinfo-export-to-texinfo
1731 nil subtreep visible-only body-only ext-plist))))
1733 (defun org-texinfo-compile (file)
1734 "Compile a texinfo file.
1736 FILE is the name of the file being compiled. Processing is
1737 done through the command specified in `org-texinfo-info-process'.
1739 Return INFO file name or an error if it couldn't be produced."
1740 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1741 (full-name (file-truename file))
1742 (out-dir (file-name-directory file))
1743 ;; Make sure `default-directory' is set to FILE directory,
1744 ;; not to whatever value the current buffer may have.
1745 (default-directory (file-name-directory full-name))
1746 errors)
1747 (message (format "Processing Texinfo file %s ..." file))
1748 (save-window-excursion
1749 (cond
1750 ;; A function is provided: Apply it.
1751 ((functionp org-texinfo-info-process)
1752 (funcall org-texinfo-info-process (shell-quote-argument file)))
1753 ;; A list is provided: Replace %b, %f and %o with appropriate
1754 ;; values in each command before applying it. Output is
1755 ;; redirected to "*Org INFO Texinfo Output*" buffer.
1756 ((consp org-texinfo-info-process)
1757 (let ((outbuf (get-buffer-create "*Org INFO Texinfo Output*")))
1758 (mapc
1759 (lambda (command)
1760 (shell-command
1761 (replace-regexp-in-string
1762 "%b" (shell-quote-argument base-name)
1763 (replace-regexp-in-string
1764 "%f" (shell-quote-argument full-name)
1765 (replace-regexp-in-string
1766 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1767 outbuf))
1768 org-texinfo-info-process)
1769 ;; Collect standard errors from output buffer.
1770 (setq errors (org-texinfo-collect-errors outbuf))))
1771 (t (error "No valid command to process to Info")))
1772 (let ((infofile (concat out-dir base-name ".info")))
1773 ;; Check for process failure. Provide collected errors if
1774 ;; possible.
1775 (if (not (file-exists-p infofile))
1776 (error (concat (format "INFO file %s wasn't produced" infofile)
1777 (when errors (concat ": " errors))))
1778 ;; Else remove log files, when specified, and signal end of
1779 ;; process to user, along with any error encountered.
1780 (message (concat "Process completed"
1781 (if (not errors) "."
1782 (concat " with errors: " errors)))))
1783 ;; Return output file name.
1784 infofile))))
1786 (defun org-texinfo-collect-errors (buffer)
1787 "Collect some kind of errors from \"makeinfo\" command output.
1789 BUFFER is the buffer containing output.
1791 Return collected error types as a string, or nil if there was
1792 none."
1793 (with-current-buffer buffer
1794 (save-excursion
1795 (goto-char (point-min))
1796 ;; Find final "makeinfo" run.
1797 (when t
1798 (let ((case-fold-search t)
1799 (errors ""))
1800 (when (save-excursion
1801 (re-search-forward "perhaps incorrect sectioning?" nil t))
1802 (setq errors (concat errors " [incorrect sectioning]")))
1803 (when (save-excursion
1804 (re-search-forward "missing close brace" nil t))
1805 (setq errors (concat errors " [syntax error]")))
1806 (when (save-excursion
1807 (re-search-forward "Unknown command" nil t))
1808 (setq errors (concat errors " [undefined @command]")))
1809 (when (save-excursion
1810 (re-search-forward "No matching @end" nil t))
1811 (setq errors (concat errors " [block incomplete]")))
1812 (when (save-excursion
1813 (re-search-forward "requires a sectioning" nil t))
1814 (setq errors (concat errors " [invalid section command]")))
1815 (when (save-excursion
1816 (re-search-forward "\\[unexpected\]" nil t))
1817 (setq errors (concat errors " [unexpected error]")))
1818 (when (save-excursion
1819 (re-search-forward "misplaced " nil t))
1820 (setq errors (concat errors " [syntax error]")))
1821 (and (org-string-nw-p errors) (org-trim errors)))))))
1824 (provide 'ox-texinfo)
1826 ;; Local variables:
1827 ;; generated-autoload-file: "org-loaddefs.el"
1828 ;; End:
1830 ;;; ox-texinfo.el ends here