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