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