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