org-element: OPTIONAL_TITLE becomes ALT_TITLE property
[org-mode.git] / lisp / ox-texinfo.el
blob9400b82cc462c6fbf0785e735982d8b0d05586ef
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-alt-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-alt-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 ;;6 is "* " ":: " for inserted text
629 length
631 org-texinfo-node-description-column
632 6)))
633 (spacing (- column length)
635 (if (> length -1)
636 (concat "* " title ":: "
637 (make-string spacing ?\s)
638 (if desc
639 (concat desc)))
640 (concat "\n" title "\n"))))
641 text-menu))
642 output))
644 ;;; Template
646 (defun org-texinfo-template (contents info)
647 "Return complete document string after Texinfo conversion.
648 CONTENTS is the transcoded contents string. INFO is a plist
649 holding export options."
650 (let* ((title (org-export-data (plist-get info :title) info))
651 (info-filename (or (plist-get info :texinfo-filename)
652 (file-name-nondirectory
653 (org-export-output-file-name ".info"))))
654 (author (org-export-data (plist-get info :author) info))
655 (texinfo-header (plist-get info :texinfo-header))
656 (texinfo-post-header (plist-get info :texinfo-post-header))
657 (subtitle (plist-get info :subtitle))
658 (subauthor (plist-get info :subauthor))
659 (class (plist-get info :texinfo-class))
660 (header (nth 1 (assoc class org-texinfo-classes)))
661 (copying
662 (org-element-map (plist-get info :parse-tree) 'headline
663 (lambda (hl) (and (org-element-property :COPYING hl) hl)) info t))
664 (dircat (plist-get info :texinfo-dircat))
665 (dirtitle (plist-get info :texinfo-dirtitle))
666 (dirdesc (plist-get info :texinfo-dirdesc))
667 ;; Spacing to align description (column 32 - 3 for `* ' and
668 ;; `.' in text.
669 (dirspacing (- 29 (length dirtitle)))
670 (menu (org-texinfo-make-menu info 'main))
671 (detail-menu (org-texinfo-make-menu info 'detailed)))
672 (concat
673 ;; Header
674 header "\n"
675 "@c %**start of header\n"
676 ;; Filename and Title
677 "@setfilename " info-filename "\n"
678 "@settitle " title "\n"
679 "\n\n"
680 "@c Version and Contact Info\n"
681 "@set AUTHOR " author "\n"
683 ;; Additional Header Options set by `#+TEXINFO_HEADER
684 (if texinfo-header
685 (concat "\n"
686 texinfo-header
687 "\n"))
689 "@c %**end of header\n"
690 "@finalout\n"
691 "\n\n"
693 ;; Additional Header Options set by #+TEXINFO_POST_HEADER
694 (if texinfo-post-header
695 (concat "\n"
696 texinfo-post-header
697 "\n"))
699 ;; Copying
700 "@copying\n"
701 ;; Only export the content of the headline, do not need the
702 ;; initial headline.
703 (org-export-data (nth 2 copying) info)
704 "@end copying\n"
705 "\n\n"
707 ;; Info directory information
708 ;; Only supply if both title and category are provided
709 (if (and dircat dirtitle)
710 (concat "@dircategory " dircat "\n"
711 "@direntry\n"
712 "* " dirtitle "."
713 (make-string dirspacing ?\s)
714 dirdesc "\n"
715 "@end direntry\n"))
716 "\n\n"
718 ;; Title
719 "@titlepage\n"
720 "@title " title "\n\n"
721 (if subtitle
722 (concat "@subtitle " subtitle "\n"))
723 "@author " author "\n"
724 (if subauthor
725 (concat subauthor "\n"))
726 "\n"
727 "@c The following two commands start the copyright page.\n"
728 "@page\n"
729 "@vskip 0pt plus 1filll\n"
730 "@insertcopying\n"
731 "@end titlepage\n\n"
732 "@c Output the table of contents at the beginning.\n"
733 "@contents\n\n"
735 ;; Configure Top Node when not for Tex
736 "@ifnottex\n"
737 "@node Top\n"
738 "@top " title " Manual\n"
739 "@insertcopying\n"
740 "@end ifnottex\n\n"
742 ;; Do not output menus if they are empty
743 (if menu
744 ;; Menu
745 (concat "@menu\n"
746 menu
747 "\n\n"
748 ;; Detailed Menu
749 (if detail-menu
750 (concat "@detailmenu\n"
751 " --- The Detailed Node Listing ---\n"
752 detail-menu
753 "\n\n"
754 "@end detailmenu\n"))
755 "@end menu\n"))
756 "\n\n"
758 ;; Document's body.
759 contents
760 "\n"
761 ;; Creator.
762 (let ((creator-info (plist-get info :with-creator)))
763 (cond
764 ((not creator-info) "")
765 ((eq creator-info 'comment)
766 (format "@c %s\n" (plist-get info :creator)))
767 (t (concat (plist-get info :creator) "\n"))))
768 ;; Document end.
769 "\n@bye")))
773 ;;; Transcode Functions
775 ;;; Bold
777 (defun org-texinfo-bold (bold contents info)
778 "Transcode BOLD from Org to Texinfo.
779 CONTENTS is the text with bold markup. INFO is a plist holding
780 contextual information."
781 (org-texinfo--text-markup contents 'bold))
783 ;;; Center Block
785 (defun org-texinfo-center-block (center-block contents info)
786 "Transcode a CENTER-BLOCK element from Org to Texinfo.
787 CONTENTS holds the contents of the block. INFO is a plist used
788 as a communication channel."
789 contents)
791 ;;; Clock
793 (defun org-texinfo-clock (clock contents info)
794 "Transcode a CLOCK element from Org to Texinfo.
795 CONTENTS is nil. INFO is a plist holding contextual
796 information."
797 (concat
798 "@noindent"
799 (format "@strong{%s} " org-clock-string)
800 (format org-texinfo-inactive-timestamp-format
801 (concat (org-translate-time
802 (org-element-property :raw-value
803 (org-element-property :value clock)))
804 (let ((time (org-element-property :duration clock)))
805 (and time (format " (%s)" time)))))
806 "@*"))
808 ;;; Code
810 (defun org-texinfo-code (code contents info)
811 "Transcode a CODE object from Org to Texinfo.
812 CONTENTS is nil. INFO is a plist used as a communication
813 channel."
814 (org-texinfo--text-markup (org-element-property :value code) 'code))
816 ;;; Comment
818 (defun org-texinfo-comment (comment contents info)
819 "Transcode a COMMENT object from Org to Texinfo.
820 CONTENTS is the text in the comment. INFO is a plist holding
821 contextual information."
822 (org-texinfo--text-markup (org-element-property :value comment) 'comment))
824 ;;; Comment Block
826 (defun org-texinfo-comment-block (comment-block contents info)
827 "Transcode a COMMENT-BLOCK object from Org to Texinfo.
828 CONTENTS is the text within the block. INFO is a plist holding
829 contextual information."
830 (format "@ignore\n%s@end ignore" (org-element-property :value comment-block)))
832 ;;; Drawer
834 (defun org-texinfo-drawer (drawer contents info)
835 "Transcode a DRAWER element from Org to Texinfo.
836 CONTENTS holds the contents of the block. INFO is a plist
837 holding contextual information."
838 (let* ((name (org-element-property :drawer-name drawer))
839 (output (if (functionp org-texinfo-format-drawer-function)
840 (funcall org-texinfo-format-drawer-function
841 name contents)
842 ;; If there's no user defined function: simply
843 ;; display contents of the drawer.
844 contents)))
845 output))
847 ;;; Dynamic Block
849 (defun org-texinfo-dynamic-block (dynamic-block contents info)
850 "Transcode a DYNAMIC-BLOCK element from Org to Texinfo.
851 CONTENTS holds the contents of the block. INFO is a plist
852 holding contextual information. See `org-export-data'."
853 contents)
855 ;;; Entity
857 (defun org-texinfo-entity (entity contents info)
858 "Transcode an ENTITY object from Org to Texinfo.
859 CONTENTS are the definition itself. INFO is a plist holding
860 contextual information."
861 (let ((ent (org-element-property :latex entity)))
862 (if (org-element-property :latex-math-p entity) (format "@math{%s}" ent) ent)))
864 ;;; Example Block
866 (defun org-texinfo-example-block (example-block contents info)
867 "Transcode an EXAMPLE-BLOCK element from Org to Texinfo.
868 CONTENTS is nil. INFO is a plist holding contextual
869 information."
870 (format "@verbatim\n%s@end verbatim"
871 (org-export-format-code-default example-block info)))
873 ;;; Export Block
875 (defun org-texinfo-export-block (export-block contents info)
876 "Transcode a EXPORT-BLOCK element from Org to Texinfo.
877 CONTENTS is nil. INFO is a plist holding contextual information."
878 (when (string= (org-element-property :type export-block) "TEXINFO")
879 (org-remove-indentation (org-element-property :value export-block))))
881 ;;; Export Snippet
883 (defun org-texinfo-export-snippet (export-snippet contents info)
884 "Transcode a EXPORT-SNIPPET object from Org to Texinfo.
885 CONTENTS is nil. INFO is a plist holding contextual information."
886 (when (eq (org-export-snippet-backend export-snippet) 'texinfo)
887 (org-element-property :value export-snippet)))
889 ;;; Fixed Width
891 (defun org-texinfo-fixed-width (fixed-width contents info)
892 "Transcode a FIXED-WIDTH element from Org to Texinfo.
893 CONTENTS is nil. INFO is a plist holding contextual information."
894 (format "@example\n%s\n@end example"
895 (org-remove-indentation
896 (org-texinfo--sanitize-content
897 (org-element-property :value fixed-width)))))
899 ;;; Footnote Reference
902 (defun org-texinfo-footnote-reference (footnote contents info)
903 "Create a footnote reference for FOOTNOTE.
905 FOOTNOTE is the footnote to define. CONTENTS is nil. INFO is a
906 plist holding contextual information."
907 (let ((def (org-export-get-footnote-definition footnote info)))
908 (format "@footnote{%s}"
909 (org-trim (org-export-data def info)))))
911 ;;; Headline
913 (defun org-texinfo-headline (headline contents info)
914 "Transcode a HEADLINE element from Org to Texinfo.
915 CONTENTS holds the contents of the headline. INFO is a plist
916 holding contextual information."
917 (let* ((class (plist-get info :texinfo-class))
918 (level (org-export-get-relative-level headline info))
919 (numberedp (org-export-numbered-headline-p headline info))
920 (class-sectionning (assoc class org-texinfo-classes))
921 ;; Find the index type, if any
922 (index (org-element-property :INDEX headline))
923 ;; Check if it is an appendix
924 (appendix (org-element-property :APPENDIX headline))
925 ;; Retrieve headline text
926 (text (org-texinfo--sanitize-headline
927 (org-element-property :title headline) info))
928 ;; Create node info, to insert it before section formatting.
929 ;; Use custom menu title if present
930 (node (format "@node %s\n" (org-texinfo--get-node headline info)))
931 ;; Menus must be generated with first child, otherwise they
932 ;; will not nest properly
933 (menu (let* ((first (org-export-first-sibling-p headline info))
934 (parent (org-export-get-parent-headline headline))
935 (title (org-texinfo--sanitize-headline
936 (org-element-property :title parent) info))
937 heading listing
938 (tree (plist-get info :parse-tree)))
939 (if first
940 (org-element-map (plist-get info :parse-tree) 'headline
941 (lambda (ref)
942 (if (member title (org-element-property :title ref))
943 (push ref heading)))
944 info t))
945 (setq listing (org-texinfo--build-menu
946 (car heading) level info))
947 (if listing
948 (setq listing (replace-regexp-in-string
949 "%" "%%" listing)
950 listing (format
951 "\n@menu\n%s\n@end menu\n\n" listing))
952 'nil)))
953 ;; Section formatting will set two placeholders: one for the
954 ;; title and the other for the contents.
955 (section-fmt
956 (let ((sec (if (and (symbolp (nth 2 class-sectionning))
957 (fboundp (nth 2 class-sectionning)))
958 (funcall (nth 2 class-sectionning) level numberedp)
959 (nth (1+ level) class-sectionning))))
960 (cond
961 ;; No section available for that LEVEL.
962 ((not sec) nil)
963 ;; Section format directly returned by a function.
964 ((stringp sec) sec)
965 ;; (numbered-section . unnumbered-section)
966 ((not (consp (cdr sec)))
967 (cond
968 ;;If an index, always unnumbered
969 (index
970 (concat menu node (cdr sec) "\n%s"))
971 (appendix
972 (concat menu node (replace-regexp-in-string
973 "unnumbered"
974 "appendix"
975 (cdr sec)) "\n%s"))
976 ;; Otherwise number as needed.
978 (concat menu node
979 (funcall
980 (if numberedp #'car #'cdr) sec) "\n%s")))))))
981 (todo
982 (and (plist-get info :with-todo-keywords)
983 (let ((todo (org-element-property :todo-keyword headline)))
984 (and todo (org-export-data todo info)))))
985 (todo-type (and todo (org-element-property :todo-type headline)))
986 (tags (and (plist-get info :with-tags)
987 (org-export-get-tags headline info)))
988 (priority (and (plist-get info :with-priority)
989 (org-element-property :priority headline)))
990 ;; Create the headline text along with a no-tag version. The
991 ;; latter is required to remove tags from table of contents.
992 (full-text (org-texinfo--sanitize-content
993 (if (functionp org-texinfo-format-headline-function)
994 ;; User-defined formatting function.
995 (funcall org-texinfo-format-headline-function
996 todo todo-type priority text tags)
997 ;; Default formatting.
998 (concat
999 (when todo
1000 (format "@strong{%s} " todo))
1001 (when priority (format "@emph{#%s} " priority))
1002 text
1003 (when tags
1004 (format ":%s:"
1005 (mapconcat 'identity tags ":")))))))
1006 (full-text-no-tag
1007 (org-texinfo--sanitize-content
1008 (if (functionp org-texinfo-format-headline-function)
1009 ;; User-defined formatting function.
1010 (funcall org-texinfo-format-headline-function
1011 todo todo-type priority text nil)
1012 ;; Default formatting.
1013 (concat
1014 (when todo (format "@strong{%s} " todo))
1015 (when priority (format "@emph{#%c} " priority))
1016 text))))
1017 (pre-blanks
1018 (make-string (org-element-property :pre-blank headline) 10)))
1019 (cond
1020 ;; Case 1: This is a footnote section: ignore it.
1021 ((org-element-property :footnote-section-p headline) nil)
1022 ;; Case 2: This is the `copying' section: ignore it
1023 ;; This is used elsewhere.
1024 ((org-element-property :COPYING headline) nil)
1025 ;; Case 3: An index. If it matches one of the known indexes,
1026 ;; print it as such following the contents, otherwise
1027 ;; print the contents and leave the index up to the user.
1028 (index
1029 (format
1030 section-fmt full-text
1031 (concat pre-blanks contents "\n"
1032 (if (member index '("cp" "fn" "ky" "pg" "tp" "vr"))
1033 (concat "@printindex " index)))))
1034 ;; Case 4: This is a deep sub-tree: export it as a list item.
1035 ;; Also export as items headlines for which no section
1036 ;; format has been found.
1037 ((or (not section-fmt) (org-export-low-level-p headline info))
1038 ;; Build the real contents of the sub-tree.
1039 (let ((low-level-body
1040 (concat
1041 ;; If the headline is the first sibling, start a list.
1042 (when (org-export-first-sibling-p headline info)
1043 (format "@%s\n" (if numberedp 'enumerate 'itemize)))
1044 ;; Itemize headline
1045 "@item\n" full-text "\n" pre-blanks contents)))
1046 ;; If headline is not the last sibling simply return
1047 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1048 ;; blank line.
1049 (if (not (org-export-last-sibling-p headline info)) low-level-body
1050 (replace-regexp-in-string
1051 "[ \t\n]*\\'"
1052 (format "\n@end %s" (if numberedp 'enumerate 'itemize))
1053 low-level-body))))
1054 ;; Case 5: Standard headline. Export it as a section.
1056 (cond
1057 ((not (and tags (eq (plist-get info :with-tags) 'not-in-toc)))
1058 ;; Regular section. Use specified format string.
1059 (format (replace-regexp-in-string "%]" "%%]" section-fmt) full-text
1060 (concat pre-blanks contents)))
1061 ((string-match "\\`@\\(.*?\\){" section-fmt)
1062 ;; If tags should be removed from table of contents, insert
1063 ;; title without tags as an alternative heading in sectioning
1064 ;; command.
1065 (format (replace-match (concat (match-string 1 section-fmt) "[%s]")
1066 nil nil section-fmt 1)
1067 ;; Replace square brackets with parenthesis since
1068 ;; square brackets are not supported in optional
1069 ;; arguments.
1070 (replace-regexp-in-string
1071 "\\[" "("
1072 (replace-regexp-in-string
1073 "\\]" ")"
1074 full-text-no-tag))
1075 full-text
1076 (concat pre-blanks contents)))
1078 ;; Impossible to add an alternative heading. Fallback to
1079 ;; regular sectioning format string.
1080 (format (replace-regexp-in-string "%]" "%%]" section-fmt) full-text
1081 (concat pre-blanks contents))))))))
1083 ;;; Inline Src Block
1085 (defun org-texinfo-inline-src-block (inline-src-block contents info)
1086 "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
1087 CONTENTS holds the contents of the item. INFO is a plist holding
1088 contextual information."
1089 (let* ((code (org-element-property :value inline-src-block))
1090 (separator (org-texinfo--find-verb-separator code)))
1091 (concat "@verb{" separator code separator "}")))
1093 ;;; Inlinetask
1095 (defun org-texinfo-inlinetask (inlinetask contents info)
1096 "Transcode an INLINETASK element from Org to Texinfo.
1097 CONTENTS holds the contents of the block. INFO is a plist
1098 holding contextual information."
1099 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1100 (todo (and (plist-get info :with-todo-keywords)
1101 (let ((todo (org-element-property :todo-keyword inlinetask)))
1102 (and todo (org-export-data todo info)))))
1103 (todo-type (org-element-property :todo-type inlinetask))
1104 (tags (and (plist-get info :with-tags)
1105 (org-export-get-tags inlinetask info)))
1106 (priority (and (plist-get info :with-priority)
1107 (org-element-property :priority inlinetask))))
1108 ;; If `org-texinfo-format-inlinetask-function' is provided, call it
1109 ;; with appropriate arguments.
1110 (if (functionp org-texinfo-format-inlinetask-function)
1111 (funcall org-texinfo-format-inlinetask-function
1112 todo todo-type priority title tags contents)
1113 ;; Otherwise, use a default template.
1114 (let ((full-title
1115 (concat
1116 (when todo (format "@strong{%s} " todo))
1117 (when priority (format "#%c " priority))
1118 title
1119 (when tags (format ":%s:"
1120 (mapconcat 'identity tags ":"))))))
1121 (format (concat "@center %s\n\n"
1122 "%s"
1123 "\n")
1124 full-title contents)))))
1126 ;;; Italic
1128 (defun org-texinfo-italic (italic contents info)
1129 "Transcode ITALIC from Org to Texinfo.
1130 CONTENTS is the text with italic markup. INFO is a plist holding
1131 contextual information."
1132 (org-texinfo--text-markup contents 'italic))
1134 ;;; Item
1136 (defun org-texinfo-item (item contents info)
1137 "Transcode an ITEM element from Org to Texinfo.
1138 CONTENTS holds the contents of the item. INFO is a plist holding
1139 contextual information."
1140 (let* ((tag (org-element-property :tag item))
1141 (desc (org-export-data tag info)))
1142 (concat "\n@item " (if tag desc) "\n"
1143 (org-trim contents) "\n")))
1145 ;;; Keyword
1147 (defun org-texinfo-keyword (keyword contents info)
1148 "Transcode a KEYWORD element from Org to Texinfo.
1149 CONTENTS is nil. INFO is a plist holding contextual information."
1150 (let ((key (org-element-property :key keyword))
1151 (value (org-element-property :value keyword)))
1152 (cond
1153 ((string= key "TEXINFO") value)
1154 ((string= key "CINDEX") (format "@cindex %s" value))
1155 ((string= key "FINDEX") (format "@findex %s" value))
1156 ((string= key "KINDEX") (format "@kindex %s" value))
1157 ((string= key "PINDEX") (format "@pindex %s" value))
1158 ((string= key "TINDEX") (format "@tindex %s" value))
1159 ((string= key "VINDEX") (format "@vindex %s" value)))))
1161 ;;; Line Break
1163 (defun org-texinfo-line-break (line-break contents info)
1164 "Transcode a LINE-BREAK object from Org to Texinfo.
1165 CONTENTS is nil. INFO is a plist holding contextual information."
1166 "@*\n")
1168 ;;; Link
1170 (defun org-texinfo-link (link desc info)
1171 "Transcode a LINK object from Org to Texinfo.
1173 DESC is the description part of the link, or the empty string.
1174 INFO is a plist holding contextual information. See
1175 `org-export-data'."
1176 (let* ((type (org-element-property :type link))
1177 (raw-path (org-element-property :path link))
1178 ;; Ensure DESC really exists, or set it to nil.
1179 (desc (and (not (string= desc "")) desc))
1180 (path (cond
1181 ((member type '("http" "https" "ftp"))
1182 (concat type ":" raw-path))
1183 ((string= type "file")
1184 (if (file-name-absolute-p raw-path)
1185 (concat "file://" (expand-file-name raw-path))
1186 (concat "file://" raw-path)))
1187 (t raw-path)))
1188 (email (if (string= type "mailto")
1189 (let ((text (replace-regexp-in-string
1190 "@" "@@" raw-path)))
1191 (concat text (if desc (concat "," desc))))))
1192 protocol)
1193 (cond
1194 ;; Links pointing to a headline: Find destination and build
1195 ;; appropriate referencing command.
1196 ((member type '("custom-id" "id"))
1197 (let ((destination (org-export-resolve-id-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 a 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 ((member type '("info"))
1213 (let* ((info-path (split-string path "[:#]"))
1214 (info-manual (car info-path))
1215 (info-node (or (cadr info-path) "top"))
1216 (title (or desc "")))
1217 (format "@ref{%s,%s,,%s,}" info-node title info-manual)))
1218 ((member type '("fuzzy"))
1219 (let ((destination (org-export-resolve-fuzzy-link link info)))
1220 (case (org-element-type destination)
1221 ;; Id link points to an external file.
1222 (plain-text
1223 (if desc (format "@uref{file://%s,%s}" destination desc)
1224 (format "@uref{file://%s}" destination)))
1225 ;; LINK points to a headline. Use the headline as the NODE target
1226 (headline
1227 (format "@ref{%s,%s}"
1228 (org-texinfo--get-node destination info)
1229 (or desc "")))
1230 (otherwise
1231 (let ((path (org-export-solidify-link-text path)))
1232 (if (not desc) (format "@ref{%s}" path)
1233 (format "@ref{%s,,%s}" path desc)))))))
1234 ;; Special case for email addresses
1235 (email
1236 (format "@email{%s}" email))
1237 ;; External link with a description part.
1238 ((and path desc) (format "@uref{%s,%s}" path desc))
1239 ;; External link without a description part.
1240 (path (format "@uref{%s}" path))
1241 ;; No path, only description. Try to do something useful.
1242 (t (format org-texinfo-link-with-unknown-path-format desc)))))
1245 ;;; Menu
1247 (defun org-texinfo-make-menu (info level)
1248 "Create the menu for inclusion in the texifo document.
1250 INFO is the parsed buffer that contains the headlines. LEVEL
1251 determines whether to make the main menu, or the detailed menu.
1253 This is only used for generating the primary menu. In-Node menus
1254 are generated directly."
1255 (let ((parse (plist-get info :parse-tree)))
1256 (cond
1257 ;; Generate the main menu
1258 ((eq level 'main) (org-texinfo--build-menu parse 1 info))
1259 ;; Generate the detailed (recursive) menu
1260 ((eq level 'detailed)
1261 ;; Requires recursion
1262 ;;(org-texinfo--build-detailed-menu parse top info)
1263 (org-texinfo--build-menu parse 1 info 'detailed)))))
1265 ;;; Paragraph
1267 (defun org-texinfo-paragraph (paragraph contents info)
1268 "Transcode a PARAGRAPH element from Org to Texinfo.
1269 CONTENTS is the contents of the paragraph, as a string. INFO is
1270 the plist used as a communication channel."
1271 contents)
1273 ;;; Plain List
1275 (defun org-texinfo-plain-list (plain-list contents info)
1276 "Transcode a PLAIN-LIST element from Org to Texinfo.
1277 CONTENTS is the contents of the list. INFO is a plist holding
1278 contextual information."
1279 (let* ((attr (org-export-read-attribute :attr_texinfo plain-list))
1280 (indic (or (plist-get attr :indic)
1281 org-texinfo-def-table-markup))
1282 (type (org-element-property :type plain-list))
1283 (table-type (or (plist-get attr :table-type)
1284 "table"))
1285 ;; Ensure valid texinfo table type.
1286 (table-type (if (memq table-type '("table" "ftable" "vtable"))
1287 table-type
1288 "table"))
1289 (list-type (cond
1290 ((eq type 'ordered) "enumerate")
1291 ((eq type 'unordered) "itemize")
1292 ((eq type 'descriptive) table-type))))
1293 (format "@%s%s\n@end %s"
1294 (if (eq type 'descriptive)
1295 (concat list-type " " indic)
1296 list-type)
1297 contents
1298 list-type)))
1300 ;;; Plain Text
1302 (defun org-texinfo-plain-text (text info)
1303 "Transcode a TEXT string from Org to Texinfo.
1304 TEXT is the string to transcode. INFO is a plist holding
1305 contextual information."
1306 ;; First protect @, { and }.
1307 (let ((output (org-texinfo--sanitize-content text)))
1308 ;; Activate smart quotes. Be sure to provide original TEXT string
1309 ;; since OUTPUT may have been modified.
1310 (when (plist-get info :with-smart-quotes)
1311 (setq output
1312 (org-export-activate-smart-quotes output :texinfo info text)))
1313 ;; LaTeX into @LaTeX{} and TeX into @TeX{}
1314 (let ((case-fold-search nil)
1315 (start 0))
1316 (while (string-match "\\(\\(?:La\\)?TeX\\)" output start)
1317 (setq output (replace-match
1318 (format "@%s{}" (match-string 1 output)) nil t output)
1319 start (match-end 0))))
1320 ;; Convert special strings.
1321 (when (plist-get info :with-special-strings)
1322 (while (string-match (regexp-quote "...") output)
1323 (setq output (replace-match "@dots{}" nil t output))))
1324 ;; Handle break preservation if required.
1325 (when (plist-get info :preserve-breaks)
1326 (setq output (replace-regexp-in-string
1327 "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" output)))
1328 ;; Return value.
1329 output))
1331 ;;; Planning
1333 (defun org-texinfo-planning (planning contents info)
1334 "Transcode a PLANNING element from Org to Texinfo.
1335 CONTENTS is nil. INFO is a plist holding contextual
1336 information."
1337 (concat
1338 "@noindent"
1339 (mapconcat
1340 'identity
1341 (delq nil
1342 (list
1343 (let ((closed (org-element-property :closed planning)))
1344 (when closed
1345 (concat
1346 (format "@strong{%s} " org-closed-string)
1347 (format org-texinfo-inactive-timestamp-format
1348 (org-translate-time
1349 (org-element-property :raw-value closed))))))
1350 (let ((deadline (org-element-property :deadline planning)))
1351 (when deadline
1352 (concat
1353 (format "@strong{%s} " org-deadline-string)
1354 (format org-texinfo-active-timestamp-format
1355 (org-translate-time
1356 (org-element-property :raw-value deadline))))))
1357 (let ((scheduled (org-element-property :scheduled planning)))
1358 (when scheduled
1359 (concat
1360 (format "@strong{%s} " org-scheduled-string)
1361 (format org-texinfo-active-timestamp-format
1362 (org-translate-time
1363 (org-element-property :raw-value scheduled))))))))
1364 " ")
1365 "@*"))
1367 ;;; Property Drawer
1369 (defun org-texinfo-property-drawer (property-drawer contents info)
1370 "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
1371 CONTENTS is nil. INFO is a plist holding contextual
1372 information."
1373 ;; The property drawer isn't exported but we want separating blank
1374 ;; lines nonetheless.
1377 ;;; Quote Block
1379 (defun org-texinfo-quote-block (quote-block contents info)
1380 "Transcode a QUOTE-BLOCK element from Org to Texinfo.
1381 CONTENTS holds the contents of the block. INFO is a plist
1382 holding contextual information."
1383 (let* ((title (org-element-property :name quote-block))
1384 (start-quote (concat "@quotation"
1385 (if title
1386 (format " %s" title)))))
1387 (format "%s\n%s@end quotation" start-quote contents)))
1389 ;;; Quote Section
1391 (defun org-texinfo-quote-section (quote-section contents info)
1392 "Transcode a QUOTE-SECTION element from Org to Texinfo.
1393 CONTENTS is nil. INFO is a plist holding contextual information."
1394 (let ((value (org-remove-indentation
1395 (org-element-property :value quote-section))))
1396 (when value (format "@verbatim\n%s@end verbatim" value))))
1398 ;;; Radio Target
1400 (defun org-texinfo-radio-target (radio-target text info)
1401 "Transcode a RADIO-TARGET object from Org to Texinfo.
1402 TEXT is the text of the target. INFO is a plist holding
1403 contextual information."
1404 (format "@anchor{%s}%s"
1405 (org-export-solidify-link-text
1406 (org-element-property :value radio-target))
1407 text))
1409 ;;; Section
1411 (defun org-texinfo-section (section contents info)
1412 "Transcode a SECTION element from Org to Texinfo.
1413 CONTENTS holds the contents of the section. INFO is a plist
1414 holding contextual information."
1415 contents)
1417 ;;; Special Block
1419 (defun org-texinfo-special-block (special-block contents info)
1420 "Transcode a SPECIAL-BLOCK element from Org to Texinfo.
1421 CONTENTS holds the contents of the block. INFO is a plist used
1422 as a communication channel."
1423 contents)
1425 ;;; Src Block
1427 (defun org-texinfo-src-block (src-block contents info)
1428 "Transcode a SRC-BLOCK element from Org to Texinfo.
1429 CONTENTS holds the contents of the item. INFO is a plist holding
1430 contextual information."
1431 (let* ((lang (org-element-property :language src-block))
1432 (lisp-p (string-match-p "lisp" lang)))
1433 (cond
1434 ;; Case 1. Lisp Block
1435 (lisp-p
1436 (format "@lisp\n%s@end lisp"
1437 (org-export-format-code-default src-block info)))
1438 ;; Case 2. Other blocks
1440 (format "@example\n%s@end example"
1441 (org-export-format-code-default src-block info))))))
1443 ;;; Statistics Cookie
1445 (defun org-texinfo-statistics-cookie (statistics-cookie contents info)
1446 "Transcode a STATISTICS-COOKIE object from Org to Texinfo.
1447 CONTENTS is nil. INFO is a plist holding contextual information."
1448 (org-element-property :value statistics-cookie))
1450 ;;; Subscript
1452 (defun org-texinfo-subscript (subscript contents info)
1453 "Transcode a SUBSCRIPT object from Org to Texinfo.
1454 CONTENTS is the contents of the object. INFO is a plist holding
1455 contextual information."
1456 (format "@math{_%s}" contents))
1458 ;;; Superscript
1460 (defun org-texinfo-superscript (superscript contents info)
1461 "Transcode a SUPERSCRIPT object from Org to Texinfo.
1462 CONTENTS is the contents of the object. INFO is a plist holding
1463 contextual information."
1464 (format "@math{^%s}" contents))
1466 ;;; Table
1468 ;; `org-texinfo-table' is the entry point for table transcoding. It
1469 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
1470 ;; delegates the job to either `org-texinfo-table--table.el-table' or
1471 ;; `org-texinfo-table--org-table' functions, depending of the type of
1472 ;; the table.
1474 ;; `org-texinfo-table--align-string' is a subroutine used to build
1475 ;; alignment string for Org tables.
1477 (defun org-texinfo-table (table contents info)
1478 "Transcode a TABLE element from Org to Texinfo.
1479 CONTENTS is the contents of the table. INFO is a plist holding
1480 contextual information."
1481 (cond
1482 ;; Case 1: verbatim table.
1483 ((or org-texinfo-tables-verbatim
1484 (let ((attr (mapconcat 'identity
1485 (org-element-property :attr_latex table)
1486 " ")))
1487 (and attr (string-match "\\<verbatim\\>" attr))))
1488 (format "@verbatim \n%s\n@end verbatim"
1489 ;; Re-create table, without affiliated keywords.
1490 (org-trim
1491 (org-element-interpret-data
1492 `(table nil ,@(org-element-contents table))))))
1493 ;; Case 2: table.el table. Convert it using appropriate tools.
1494 ((eq (org-element-property :type table) 'table.el)
1495 (org-texinfo-table--table.el-table table contents info))
1496 ;; Case 3: Standard table.
1497 (t (org-texinfo-table--org-table table contents info))))
1499 (defun org-texinfo-table-column-widths (table info)
1500 "Determine the largest table cell in each column to process alignment.
1502 TABLE is the table element to transcode. INFO is a plist used as
1503 a communication channel."
1504 (let* ((rows (org-element-map table 'table-row 'identity info))
1505 (collected (loop for row in rows collect
1506 (org-element-map row 'table-cell 'identity info)))
1507 (number-cells (length (car collected)))
1508 cells counts)
1509 (loop for row in collected do
1510 (push (mapcar (lambda (ref)
1511 (let* ((start (org-element-property :contents-begin ref))
1512 (end (org-element-property :contents-end ref))
1513 (length (- end start)))
1514 length)) row) cells))
1515 (setq cells (org-remove-if 'null cells))
1516 (push (loop for count from 0 to (- number-cells 1) collect
1517 (loop for item in cells collect
1518 (nth count item))) counts)
1519 (mapconcat (lambda (size)
1520 (make-string size ?a)) (mapcar (lambda (ref)
1521 (apply 'max `,@ref)) (car counts))
1522 "} {")))
1524 (defun org-texinfo-table--org-table (table contents info)
1525 "Return appropriate Texinfo code for an Org table.
1527 TABLE is the table type element to transcode. CONTENTS is its
1528 contents, as a string. INFO is a plist used as a communication
1529 channel.
1531 This function assumes TABLE has `org' as its `:type' attribute."
1532 (let* ((attr (org-export-read-attribute :attr_texinfo table))
1533 (col-width (plist-get attr :columns))
1534 (columns (if col-width
1535 (format "@columnfractions %s"
1536 col-width)
1537 (format "{%s}"
1538 (org-texinfo-table-column-widths
1539 table info)))))
1540 ;; Prepare the final format string for the table.
1541 (cond
1542 ;; Longtable.
1543 ;; Others.
1544 (t (concat
1545 (format "@multitable %s\n%s@end multitable"
1546 columns
1547 contents))))))
1549 (defun org-texinfo-table--table.el-table (table contents info)
1550 "Returns nothing.
1552 Rather than return an invalid table, nothing is returned."
1553 'nil)
1555 ;;; Table Cell
1557 (defun org-texinfo-table-cell (table-cell contents info)
1558 "Transcode a TABLE-CELL element from Org to Texinfo.
1559 CONTENTS is the cell contents. INFO is a plist used as
1560 a communication channel."
1561 (concat (if (and contents
1562 org-texinfo-table-scientific-notation
1563 (string-match orgtbl-exp-regexp contents))
1564 ;; Use appropriate format string for scientific
1565 ;; notation.
1566 (format org-texinfo-table-scientific-notation
1567 (match-string 1 contents)
1568 (match-string 2 contents))
1569 contents)
1570 (when (org-export-get-next-element table-cell info) "\n@tab ")))
1572 ;;; Table Row
1574 (defun org-texinfo-table-row (table-row contents info)
1575 "Transcode a TABLE-ROW element from Org to Texinfo.
1576 CONTENTS is the contents of the row. INFO is a plist used as
1577 a communication channel."
1578 ;; Rules are ignored since table separators are deduced from
1579 ;; borders of the current row.
1580 (when (eq (org-element-property :type table-row) 'standard)
1581 (let ((rowgroup-tag
1582 (cond
1583 ;; Case 1: Belongs to second or subsequent rowgroup.
1584 ((not (= 1 (org-export-table-row-group table-row info)))
1585 "@item ")
1586 ;; Case 2: Row is from first rowgroup. Table has >=1 rowgroups.
1587 ((org-export-table-has-header-p
1588 (org-export-get-parent-table table-row) info)
1589 "@headitem ")
1590 ;; Case 3: Row is from first and only row group.
1591 (t "@item "))))
1592 (when (eq (org-element-property :type table-row) 'standard)
1593 (concat rowgroup-tag contents "\n")))))
1595 ;;; Target
1597 (defun org-texinfo-target (target contents info)
1598 "Transcode a TARGET object from Org to Texinfo.
1599 CONTENTS is nil. INFO is a plist holding contextual
1600 information."
1601 (format "@anchor{%s}"
1602 (org-export-solidify-link-text (org-element-property :value target))))
1604 ;;; Timestamp
1606 (defun org-texinfo-timestamp (timestamp contents info)
1607 "Transcode a TIMESTAMP object from Org to Texinfo.
1608 CONTENTS is nil. INFO is a plist holding contextual
1609 information."
1610 (let ((value (org-texinfo-plain-text
1611 (org-timestamp-translate timestamp) info)))
1612 (case (org-element-property :type timestamp)
1613 ((active active-range)
1614 (format org-texinfo-active-timestamp-format value))
1615 ((inactive inactive-range)
1616 (format org-texinfo-inactive-timestamp-format value))
1617 (t (format org-texinfo-diary-timestamp-format value)))))
1619 ;;; Verbatim
1621 (defun org-texinfo-verbatim (verbatim contents info)
1622 "Transcode a VERBATIM object from Org to Texinfo.
1623 CONTENTS is nil. INFO is a plist used as a communication
1624 channel."
1625 (org-texinfo--text-markup (org-element-property :value verbatim) 'verbatim))
1627 ;;; Verse Block
1629 (defun org-texinfo-verse-block (verse-block contents info)
1630 "Transcode a VERSE-BLOCK element from Org to Texinfo.
1631 CONTENTS is verse block contents. INFO is a plist holding
1632 contextual information."
1633 ;; In a verse environment, add a line break to each newline
1634 ;; character and change each white space at beginning of a line
1635 ;; into a space of 1 em. Also change each blank line with
1636 ;; a vertical space of 1 em.
1637 (progn
1638 (setq contents (replace-regexp-in-string
1639 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
1640 (replace-regexp-in-string
1641 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
1642 (while (string-match "^[ \t]+" contents)
1643 (let ((new-str (format "\\hspace*{%dem}"
1644 (length (match-string 0 contents)))))
1645 (setq contents (replace-match new-str nil t contents))))
1646 (format "\\begin{verse}\n%s\\end{verse}" contents)))
1649 ;;; Interactive functions
1651 (defun org-texinfo-export-to-texinfo
1652 (&optional async subtreep visible-only body-only ext-plist)
1653 "Export current buffer to a Texinfo file.
1655 If narrowing is active in the current buffer, only export its
1656 narrowed part.
1658 If a region is active, export that region.
1660 A non-nil optional argument ASYNC means the process should happen
1661 asynchronously. The resulting file should be accessible through
1662 the `org-export-stack' interface.
1664 When optional argument SUBTREEP is non-nil, export the sub-tree
1665 at point, extracting information from the headline properties
1666 first.
1668 When optional argument VISIBLE-ONLY is non-nil, don't export
1669 contents of hidden elements.
1671 When optional argument BODY-ONLY is non-nil, only write code
1672 between \"\\begin{document}\" and \"\\end{document}\".
1674 EXT-PLIST, when provided, is a property list with external
1675 parameters overriding Org default settings, but still inferior to
1676 file-local settings.
1678 Return output file's name."
1679 (interactive)
1680 (let ((outfile (org-export-output-file-name ".texi" subtreep)))
1681 (if async
1682 (org-export-async-start
1683 (lambda (f) (org-export-add-to-stack f 'texinfo))
1684 `(expand-file-name
1685 (org-export-to-file
1686 'texinfo ,outfile ,subtreep ,visible-only ,body-only
1687 ',ext-plist)))
1688 (org-export-to-file
1689 'texinfo outfile subtreep visible-only body-only ext-plist))))
1691 (defun org-texinfo-export-to-info
1692 (&optional async subtreep visible-only body-only ext-plist)
1693 "Export current buffer to Texinfo then process through to INFO.
1695 If narrowing is active in the current buffer, only export its
1696 narrowed part.
1698 If a region is active, export that region.
1700 A non-nil optional argument ASYNC means the process should happen
1701 asynchronously. The resulting file should be accessible through
1702 the `org-export-stack' interface.
1704 When optional argument SUBTREEP is non-nil, export the sub-tree
1705 at point, extracting information from the headline properties
1706 first.
1708 When optional argument VISIBLE-ONLY is non-nil, don't export
1709 contents of hidden elements.
1711 When optional argument BODY-ONLY is non-nil, only write code
1712 between \"\\begin{document}\" and \"\\end{document}\".
1714 EXT-PLIST, when provided, is a property list with external
1715 parameters overriding Org default settings, but still inferior to
1716 file-local settings.
1718 When optional argument PUB-DIR is set, use it as the publishing
1719 directory.
1721 Return INFO file's name."
1722 (interactive)
1723 (if async
1724 (let ((outfile (org-export-output-file-name ".texi" subtreep)))
1725 (org-export-async-start
1726 (lambda (f) (org-export-add-to-stack f 'texinfo))
1727 `(expand-file-name
1728 (org-texinfo-compile
1729 (org-export-to-file
1730 'texinfo ,outfile ,subtreep ,visible-only ,body-only
1731 ',ext-plist)))))
1732 (org-texinfo-compile
1733 (org-texinfo-export-to-texinfo
1734 nil subtreep visible-only body-only ext-plist))))
1736 (defun org-texinfo-compile (file)
1737 "Compile a texinfo file.
1739 FILE is the name of the file being compiled. Processing is
1740 done through the command specified in `org-texinfo-info-process'.
1742 Return INFO file name or an error if it couldn't be produced."
1743 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1744 (full-name (file-truename file))
1745 (out-dir (file-name-directory file))
1746 ;; Make sure `default-directory' is set to FILE directory,
1747 ;; not to whatever value the current buffer may have.
1748 (default-directory (file-name-directory full-name))
1749 errors)
1750 (message (format "Processing Texinfo file %s ..." file))
1751 (save-window-excursion
1752 (cond
1753 ;; A function is provided: Apply it.
1754 ((functionp org-texinfo-info-process)
1755 (funcall org-texinfo-info-process (shell-quote-argument file)))
1756 ;; A list is provided: Replace %b, %f and %o with appropriate
1757 ;; values in each command before applying it. Output is
1758 ;; redirected to "*Org INFO Texinfo Output*" buffer.
1759 ((consp org-texinfo-info-process)
1760 (let ((outbuf (get-buffer-create "*Org INFO Texinfo Output*")))
1761 (mapc
1762 (lambda (command)
1763 (shell-command
1764 (replace-regexp-in-string
1765 "%b" (shell-quote-argument base-name)
1766 (replace-regexp-in-string
1767 "%f" (shell-quote-argument full-name)
1768 (replace-regexp-in-string
1769 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1770 outbuf))
1771 org-texinfo-info-process)
1772 ;; Collect standard errors from output buffer.
1773 (setq errors (org-texinfo-collect-errors outbuf))))
1774 (t (error "No valid command to process to Info")))
1775 (let ((infofile (concat out-dir base-name ".info")))
1776 ;; Check for process failure. Provide collected errors if
1777 ;; possible.
1778 (if (not (file-exists-p infofile))
1779 (error (concat (format "INFO file %s wasn't produced" infofile)
1780 (when errors (concat ": " errors))))
1781 ;; Else remove log files, when specified, and signal end of
1782 ;; process to user, along with any error encountered.
1783 (message (concat "Process completed"
1784 (if (not errors) "."
1785 (concat " with errors: " errors)))))
1786 ;; Return output file name.
1787 infofile))))
1789 (defun org-texinfo-collect-errors (buffer)
1790 "Collect some kind of errors from \"makeinfo\" command output.
1792 BUFFER is the buffer containing output.
1794 Return collected error types as a string, or nil if there was
1795 none."
1796 (with-current-buffer buffer
1797 (save-excursion
1798 (goto-char (point-min))
1799 ;; Find final "makeinfo" run.
1800 (when t
1801 (let ((case-fold-search t)
1802 (errors ""))
1803 (when (save-excursion
1804 (re-search-forward "perhaps incorrect sectioning?" nil t))
1805 (setq errors (concat errors " [incorrect sectioning]")))
1806 (when (save-excursion
1807 (re-search-forward "missing close brace" nil t))
1808 (setq errors (concat errors " [syntax error]")))
1809 (when (save-excursion
1810 (re-search-forward "Unknown command" nil t))
1811 (setq errors (concat errors " [undefined @command]")))
1812 (when (save-excursion
1813 (re-search-forward "No matching @end" nil t))
1814 (setq errors (concat errors " [block incomplete]")))
1815 (when (save-excursion
1816 (re-search-forward "requires a sectioning" nil t))
1817 (setq errors (concat errors " [invalid section command]")))
1818 (when (save-excursion
1819 (re-search-forward "\\[unexpected\]" nil t))
1820 (setq errors (concat errors " [unexpected error]")))
1821 (when (save-excursion
1822 (re-search-forward "misplaced " nil t))
1823 (setq errors (concat errors " [syntax error]")))
1824 (and (org-string-nw-p errors) (org-trim errors)))))))
1827 (provide 'ox-texinfo)
1829 ;; Local variables:
1830 ;; generated-autoload-file: "org-loaddefs.el"
1831 ;; End:
1833 ;;; ox-texinfo.el ends here