ox-texinfo: Allow to set a different title for hard copy
[org-mode.git] / lisp / ox-texinfo.el
blob3bfb05989a1a9ba447f321a606aefef2e456a9ce
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)
132 (:texinfo-printed-title "TEXINFO_PRINTED_TITLE" nil nil t)))
136 ;;; User Configurable Variables
138 (defgroup org-export-texinfo nil
139 "Options for exporting Org mode files to Texinfo."
140 :tag "Org Export Texinfo"
141 :version "24.4"
142 :package-version '(Org . "8.0")
143 :group 'org-export)
145 ;;; Preamble
147 (defcustom org-texinfo-coding-system nil
148 "Default document encoding for Texinfo output.
150 If `nil' it will default to `buffer-file-coding-system'."
151 :group 'org-export-texinfo
152 :type 'coding-system)
154 (defcustom org-texinfo-default-class "info"
155 "The default Texinfo class."
156 :group 'org-export-texinfo
157 :type '(string :tag "Texinfo class"))
159 (defcustom org-texinfo-classes
160 '(("info"
161 "@documentencoding AUTO\n@documentlanguage AUTO"
162 ("@chapter %s" . "@unnumbered %s")
163 ("@section %s" . "@unnumberedsec %s")
164 ("@subsection %s" . "@unnumberedsubsec %s")
165 ("@subsubsection %s" . "@unnumberedsubsubsec %s")))
166 "Alist of Texinfo classes and associated header and structure.
167 If #+TEXINFO_CLASS is set in the buffer, use its value and the
168 associated information. Here is the structure of each cell:
170 \(class-name
171 header-string
172 \(numbered-section . unnumbered-section)
173 ...)
176 The header string
177 -----------------
179 The header string is inserted in the header of the generated
180 document, right after \"@setfilename\" and \"@settitle\"
181 commands.
183 If it contains the special string
185 \"@documentencoding AUTO\"
187 \"AUTO\" will be replaced with an appropriate coding system. See
188 `org-texinfo-coding-system' for more information. Likewise, if
189 the string contains the special string
191 \"@documentlanguage AUTO\"
193 \"AUTO\" will be replaced with the language defined in the
194 buffer, through #+LANGUAGE keyword, or globally, with
195 `org-export-default-language', which see.
198 The sectioning structure
199 ------------------------
201 The sectioning structure of the class is given by the elements
202 following the header string. For each sectioning level, a number
203 of strings is specified. A %s formatter is mandatory in each
204 section string and will be replaced by the title of the section.
206 Instead of a list of sectioning commands, you can also specify
207 a function name. That function will be called with two
208 parameters, the reduced) level of the headline, and a predicate
209 non-nil when the headline should be numbered. It must return
210 a format string in which the section title will be added."
211 :group 'org-export-texinfo
212 :version "24.4"
213 :package-version '(Org . "8.2")
214 :type '(repeat
215 (list (string :tag "Texinfo class")
216 (string :tag "Texinfo header")
217 (repeat :tag "Levels" :inline t
218 (choice
219 (cons :tag "Heading"
220 (string :tag " numbered")
221 (string :tag "unnumbered"))
222 (function :tag "Hook computing sectioning"))))))
224 ;;; Headline
226 (defcustom org-texinfo-format-headline-function 'ignore
227 "Function to format headline text.
229 This function will be called with 5 arguments:
230 TODO the todo keyword (string or nil).
231 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
232 PRIORITY the priority of the headline (integer or nil)
233 TEXT the main headline text (string).
234 TAGS the tags as a list of strings (list of strings or nil).
236 The function result will be used in the section format string.
238 As an example, one could set the variable to the following, in
239 order to reproduce the default set-up:
241 \(defun org-texinfo-format-headline (todo todo-type priority text tags)
242 \"Default format function for a headline.\"
243 \(concat (when todo
244 \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo))
245 \(when priority
246 \(format \"\\\\framebox{\\\\#%c} \" priority))
247 text
248 \(when tags
249 \(format \"\\\\hfill{}\\\\textsc{%s}\"
250 \(mapconcat 'identity tags \":\"))))"
251 :group 'org-export-texinfo
252 :type 'function)
254 ;;; Node listing (menu)
256 (defcustom org-texinfo-node-description-column 32
257 "Column at which to start the description in the node listings.
258 If a node title is greater than this length, the description will
259 be placed after the end of the title."
260 :group 'org-export-texinfo
261 :type 'integer)
263 ;;; Footnotes
265 ;; Footnotes are inserted directly
267 ;;; Timestamps
269 (defcustom org-texinfo-active-timestamp-format "@emph{%s}"
270 "A printf format string to be applied to active timestamps."
271 :group 'org-export-texinfo
272 :type 'string)
274 (defcustom org-texinfo-inactive-timestamp-format "@emph{%s}"
275 "A printf format string to be applied to inactive timestamps."
276 :group 'org-export-texinfo
277 :type 'string)
279 (defcustom org-texinfo-diary-timestamp-format "@emph{%s}"
280 "A printf format string to be applied to diary timestamps."
281 :group 'org-export-texinfo
282 :type 'string)
284 ;;; Links
286 (defcustom org-texinfo-link-with-unknown-path-format "@indicateurl{%s}"
287 "Format string for links with unknown path type."
288 :group 'org-export-texinfo
289 :type 'string)
291 ;;; Tables
293 (defcustom org-texinfo-tables-verbatim nil
294 "When non-nil, tables are exported verbatim."
295 :group 'org-export-texinfo
296 :type 'boolean)
298 (defcustom org-texinfo-table-scientific-notation "%s\\,(%s)"
299 "Format string to display numbers in scientific notation.
300 The format should have \"%s\" twice, for mantissa and exponent
301 \(i.e. \"%s\\\\times10^{%s}\").
303 When nil, no transformation is made."
304 :group 'org-export-texinfo
305 :type '(choice
306 (string :tag "Format string")
307 (const :tag "No formatting" nil)))
309 (defcustom org-texinfo-def-table-markup "@samp"
310 "Default setting for @table environments."
311 :group 'org-export-texinfo
312 :type 'string)
314 ;;; Text markup
316 (defcustom org-texinfo-text-markup-alist '((bold . "@strong{%s}")
317 (code . code)
318 (italic . "@emph{%s}")
319 (verbatim . verb)
320 (comment . "@c %s"))
321 "Alist of Texinfo expressions to convert text markup.
323 The key must be a symbol among `bold', `italic' and `comment'.
324 The value is a formatting string to wrap fontified text with.
326 Value can also be set to the following symbols: `verb' and
327 `code'. For the former, Org will use \"@verb\" to
328 create a format string and select a delimiter character that
329 isn't in the string. For the latter, Org will use \"@code\"
330 to typeset and try to protect special characters.
332 If no association can be found for a given markup, text will be
333 returned as-is."
334 :group 'org-export-texinfo
335 :type 'alist
336 :options '(bold code italic verbatim comment))
338 ;;; Drawers
340 (defcustom org-texinfo-format-drawer-function
341 (lambda (name contents) contents)
342 "Function called to format a drawer in Texinfo code.
344 The function must accept two parameters:
345 NAME the drawer name, like \"LOGBOOK\"
346 CONTENTS the contents of the drawer.
348 The function should return the string to be exported.
350 The default function simply returns the value of CONTENTS."
351 :group 'org-export-texinfo
352 :version "24.4"
353 :package-version '(Org . "8.2")
354 :type 'function)
356 ;;; Inlinetasks
358 (defcustom org-texinfo-format-inlinetask-function 'ignore
359 "Function called to format an inlinetask in Texinfo code.
361 The function must accept six parameters:
362 TODO the todo keyword, as a string
363 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
364 PRIORITY the inlinetask priority, as a string
365 NAME the inlinetask name, as a string.
366 TAGS the inlinetask tags, as a list of strings.
367 CONTENTS the contents of the inlinetask, as a string.
369 The function should return the string to be exported.
371 For example, the variable could be set to the following function
372 in order to mimic default behavior:
374 \(defun org-texinfo-format-inlinetask \(todo type priority name tags contents\)
375 \"Format an inline task element for Texinfo export.\"
376 \(let ((full-title
377 \(concat
378 \(when todo
379 \(format \"@strong{%s} \" todo))
380 \(when priority (format \"#%c \" priority))
381 title
382 \(when tags
383 \(format \":%s:\"
384 \(mapconcat 'identity tags \":\")))))
385 \(format (concat \"@center %s\n\n\"
386 \"%s\"
387 \"\n\"))
388 full-title contents))"
389 :group 'org-export-texinfo
390 :type 'function)
392 ;;; Src blocks
394 ;; Src Blocks are example blocks, except for LISP
396 ;;; Compilation
398 (defcustom org-texinfo-info-process
399 '("makeinfo %f")
400 "Commands to process a Texinfo file to an INFO file.
401 This is list of strings, each of them will be given to the shell
402 as a command. %f in the command will be replaced by the full
403 file name, %b by the file base name \(i.e without extension) and
404 %o by the base directory of the file."
405 :group 'org-export-texinfo
406 :type '(repeat :tag "Shell command sequence"
407 (string :tag "Shell command")))
409 (defcustom org-texinfo-logfiles-extensions
410 '("aux" "toc" "cp" "fn" "ky" "pg" "tp" "vr")
411 "The list of file extensions to consider as Texinfo logfiles.
412 The logfiles will be remove if `org-texinfo-remove-logfiles' is
413 non-nil."
414 :group 'org-export-texinfo
415 :type '(repeat (string :tag "Extension")))
417 (defcustom org-texinfo-remove-logfiles t
418 "Non-nil means remove the logfiles produced by compiling a Texinfo file.
419 By default, logfiles are files with these extensions: .aux, .toc,
420 .cp, .fn, .ky, .pg and .tp. To define the set of logfiles to remove,
421 set `org-texinfo-logfiles-extensions'."
422 :group 'org-export-latex
423 :type 'boolean)
426 ;;; Constants
427 (defconst org-texinfo-max-toc-depth 4
428 "Maximum depth for creation of detailed menu listings. Beyond
429 this depth Texinfo will not recognize the nodes and will cause
430 errors. Left as a constant in case this value ever changes.")
432 (defconst org-texinfo-supported-coding-systems
433 '("US-ASCII" "UTF-8" "ISO-8859-15" "ISO-8859-1" "ISO-8859-2" "koi8-r" "koi8-u")
434 "List of coding systems supported by Texinfo, as strings.
435 Specified coding system will be matched against these strings.
436 If two strings share the same prefix (e.g. \"ISO-8859-1\" and
437 \"ISO-8859-15\"), the most specific one has to be listed first.")
440 ;;; Internal Functions
442 (defun org-texinfo-filter-section-blank-lines (headline back-end info)
443 "Filter controlling number of blank lines after a section."
444 (let ((blanks (make-string 2 ?\n)))
445 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline)))
447 (defun org-texinfo--find-verb-separator (s)
448 "Return a character not used in string S.
449 This is used to choose a separator for constructs like \\verb."
450 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
451 (loop for c across ll
452 when (not (string-match (regexp-quote (char-to-string c)) s))
453 return (char-to-string c))))
455 (defun org-texinfo--make-option-string (options)
456 "Return a comma separated string of keywords and values.
457 OPTIONS is an alist where the key is the options keyword as
458 a string, and the value a list containing the keyword value, or
459 nil."
460 (mapconcat (lambda (pair)
461 (concat (first pair)
462 (when (> (length (second pair)) 0)
463 (concat "=" (second pair)))))
464 options
465 ","))
467 (defun org-texinfo--text-markup (text markup)
468 "Format TEXT depending on MARKUP text markup.
469 See `org-texinfo-text-markup-alist' for details."
470 (let ((fmt (cdr (assq markup org-texinfo-text-markup-alist))))
471 (cond
472 ;; No format string: Return raw text.
473 ((not fmt) text)
474 ((eq 'verb fmt)
475 (let ((separator (org-texinfo--find-verb-separator text)))
476 (concat "@verb{" separator text separator "}")))
477 ((eq 'code fmt)
478 (let ((start 0)
479 (rtn "")
480 char)
481 (while (string-match "[@{}]" text)
482 (setq char (match-string 0 text))
483 (if (> (match-beginning 0) 0)
484 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
485 (setq text (substring text (1+ (match-beginning 0))))
486 (setq char (concat "@" char)
487 rtn (concat rtn char)))
488 (setq text (concat rtn text)
489 fmt "@code{%s}")
490 (format fmt text)))
491 ;; Else use format string.
492 (t (format fmt text)))))
494 (defun org-texinfo--get-node (headline info)
495 "Return node entry associated to HEADLINE.
496 INFO is a plist used as a communication channel."
497 (let ((menu-title (org-export-get-alt-title headline info)))
498 (org-texinfo--sanitize-menu
499 (replace-regexp-in-string
500 "%" "%%"
501 (if menu-title (org-export-data menu-title info)
502 (org-texinfo--sanitize-headline
503 (org-element-property :title headline) info))))))
505 ;;; Headline sanitizing
507 (defun org-texinfo--sanitize-headline (headline info)
508 "Remove all formatting from the text of a headline for use in
509 node and menu listing."
510 (mapconcat 'identity
511 (org-texinfo--sanitize-headline-contents headline info) " "))
513 (defun org-texinfo--sanitize-headline-contents (headline info)
514 "Retrieve the content of the headline.
516 Any content that can contain further formatting is checked
517 recursively, to ensure that nested content is also properly
518 retrieved."
519 (loop for contents in headline append
520 (cond
521 ;; already a string
522 ((stringp contents)
523 (list (replace-regexp-in-string " $" "" contents)))
524 ;; Is exported as-is (value)
525 ((org-element-map contents '(verbatim code)
526 (lambda (value) (org-element-property :value value)) info))
527 ;; Has content and recurse into the content
528 ((org-element-contents contents)
529 (org-texinfo--sanitize-headline-contents
530 (org-element-contents contents) info)))))
532 ;;; Menu sanitizing
534 (defun org-texinfo--sanitize-menu (title)
535 "Remove invalid characters from TITLE for use in menus and
536 nodes.
538 Based on Texinfo specifications, the following must be removed:
539 @ { } ( ) : . ,"
540 (replace-regexp-in-string "[@{}():,.]" "" title))
542 ;;; Content sanitizing
544 (defun org-texinfo--sanitize-content (text)
545 "Ensure characters are properly escaped when used in headlines or blocks.
547 Escape characters are: @ { }"
548 (replace-regexp-in-string "\\\([@{}]\\\)" "@\\1" text))
550 ;;; Menu creation
552 (defun org-texinfo--build-menu (tree level info &optional detailed)
553 "Create the @menu/@end menu information from TREE at headline
554 level LEVEL.
556 TREE contains the parse-tree to work with, either of the entire
557 document or of a specific parent headline. LEVEL indicates what
558 level of headlines to look at when generating the menu. INFO is
559 a plist containing contextual information.
561 Detailed determines whether to build a single level of menu, or
562 recurse into all children as well."
563 (let ((menu (org-texinfo--generate-menu-list tree level info))
564 output text-menu)
565 (cond
566 (detailed
567 ;; Looping is done within the menu generation.
568 (setq text-menu (org-texinfo--generate-detailed menu level info)))
570 (setq text-menu (org-texinfo--generate-menu-items menu info))))
571 (when text-menu
572 (setq output (org-texinfo--format-menu text-menu))
573 (mapconcat 'identity output "\n"))))
575 (defun org-texinfo--generate-detailed (menu level info)
576 "Generate a detailed listing of all subheadings within MENU starting at LEVEL.
578 MENU is the parse-tree to work with. LEVEL is the starting level
579 for the menu headlines and from which recursion occurs. INFO is
580 a plist containing contextual information."
581 (when level
582 (let ((max-depth (min org-texinfo-max-toc-depth
583 (plist-get info :headline-levels))))
584 (when (> max-depth level)
585 (loop for headline in menu append
586 (let* ((title (org-texinfo--menu-headlines headline info))
587 ;; Create list of menu entries for the next level
588 (sublist (org-texinfo--generate-menu-list
589 headline (1+ level) info))
590 ;; Generate the menu items for that level. If
591 ;; there are none omit that heading completely,
592 ;; otherwise join the title to it's related entries.
593 (submenu (if (org-texinfo--generate-menu-items sublist info)
594 (append (list title)
595 (org-texinfo--generate-menu-items sublist info))
596 'nil))
597 ;; Start the process over the next level down.
598 (recursion (org-texinfo--generate-detailed sublist (1+ level) info)))
599 (setq recursion (append submenu recursion))
600 recursion))))))
602 (defun org-texinfo--generate-menu-list (tree level info)
603 "Generate the list of headlines that are within a given level
604 of the tree for further formatting.
606 TREE is the parse-tree containing the headlines. LEVEL is the
607 headline level to generate a list of. INFO is a plist holding
608 contextual information."
609 (org-element-map tree 'headline
610 (lambda (head)
611 (and (= (org-export-get-relative-level head info) level)
612 ;; Do not take note of footnotes or copying headlines.
613 (not (org-element-property :COPYING head))
614 (not (org-element-property :footnote-section-p head))
615 ;; Collect headline.
616 head))
617 info))
619 (defun org-texinfo--generate-menu-items (items info)
620 "Generate a list of headline information from the listing ITEMS.
622 ITEMS is a list of the headlines to be converted into entries.
623 INFO is a plist containing contextual information.
625 Returns a list containing the following information from each
626 headline: length, title, description. This is used to format the
627 menu using `org-texinfo--format-menu'."
628 (loop for headline in items collect
629 (let* ((menu-title (org-texinfo--sanitize-menu
630 (org-export-data
631 (org-export-get-alt-title headline info)
632 info)))
633 (title (org-texinfo--sanitize-menu
634 (org-texinfo--sanitize-headline
635 (org-element-property :title headline) info)))
636 (descr (org-export-data
637 (org-element-property :DESCRIPTION headline)
638 info))
639 (menu-entry (if (string= "" menu-title) title menu-title))
640 (len (length menu-entry))
641 (output (list len menu-entry descr)))
642 output)))
644 (defun org-texinfo--menu-headlines (headline info)
645 "Retrieve the title from HEADLINE.
647 INFO is a plist holding contextual information.
649 Return the headline as a list of (length title description) with
650 length of -1 and nil description. This is used in
651 `org-texinfo--format-menu' to identify headlines as opposed to
652 entries."
653 (let ((title (org-export-data
654 (org-element-property :title headline) info)))
655 (list -1 title 'nil)))
657 (defun org-texinfo--format-menu (text-menu)
658 "Format the TEXT-MENU items to be properly printed in the menu.
660 Each entry in the menu should be provided as (length title
661 description).
663 Headlines in the detailed menu are given length -1 to ensure they
664 are never confused with other entries. They also have no
665 description.
667 Other menu items are output as:
668 Title:: description
670 With the spacing between :: and description based on the length
671 of the longest menu entry."
673 (let (output)
674 (setq output
675 (mapcar (lambda (name)
676 (let* ((title (nth 1 name))
677 (desc (nth 2 name))
678 (length (nth 0 name))
679 (column (max
680 ;;6 is "* " ":: " for inserted text
681 length
683 org-texinfo-node-description-column
684 6)))
685 (spacing (- column length)
687 (if (> length -1)
688 (concat "* " title ":: "
689 (make-string spacing ?\s)
690 (if desc
691 (concat desc)))
692 (concat "\n" title "\n"))))
693 text-menu))
694 output))
696 ;;; Template
698 (defun org-texinfo-template (contents info)
699 "Return complete document string after Texinfo conversion.
700 CONTENTS is the transcoded contents string. INFO is a plist
701 holding export options."
702 (let ((title (org-export-data (plist-get info :title) info))
703 ;; Copying data is the contents of the first headline in
704 ;; parse tree with a non-nil copying property.
705 (copying (org-element-map (plist-get info :parse-tree) 'headline
706 (lambda (hl)
707 (and (org-not-nil (org-element-property :COPYING hl))
708 (org-element-contents hl)))
709 info t)))
710 (concat
711 "\\input texinfo @c -*- texinfo -*-\n"
712 "@c %**start of header\n"
713 (let ((file (or (plist-get info :texinfo-filename)
714 (let ((f (plist-get info :output-file)))
715 (and f (concat (file-name-sans-extension f) ".info"))))))
716 (and file (format "@setfilename %s\n" file)))
717 (format "@settitle %s\n" title)
718 ;; Insert class-defined header.
719 (org-element-normalize-string
720 (let ((header (nth 1 (assoc (plist-get info :texinfo-class)
721 org-texinfo-classes)))
722 (coding
723 (catch 'coding-system
724 (let ((case-fold-search t)
725 (name (symbol-name (or org-texinfo-coding-system
726 buffer-file-coding-system))))
727 (dolist (system org-texinfo-supported-coding-systems "UTF-8")
728 (when (org-string-match-p (regexp-quote system) name)
729 (throw 'coding-system system))))))
730 (language (plist-get info :language))
731 (case-fold-search nil))
732 ;; Auto coding system.
733 (replace-regexp-in-string
734 "^@documentencoding \\(AUTO\\)$"
735 coding
736 (replace-regexp-in-string
737 "^@documentlanguage \\(AUTO\\)$" language header nil nil 1)
738 nil nil 1)))
739 ;; Additional header options set by #+TEXINFO_HEADER.
740 (let ((texinfo-header (plist-get info :texinfo-header)))
741 (and texinfo-header (org-element-normalize-string texinfo-header)))
742 "@c %**end of header\n\n"
743 ;; Additional options set by #+TEXINFO_POST_HEADER.
744 (let ((texinfo-post-header (plist-get info :texinfo-post-header)))
745 (and texinfo-post-header
746 (org-element-normalize-string texinfo-post-header)))
747 ;; Copying.
748 (and copying
749 (format "@copying\n%s@end copying\n\n"
750 (org-element-normalize-string
751 (org-export-data copying info))))
752 ;; Info directory information. Only supply if both title and
753 ;; category are provided.
754 (let ((dircat (plist-get info :texinfo-dircat))
755 (dirtitle
756 (let ((title (plist-get info :texinfo-dirtitle)))
757 (and title
758 (string-match "^\\(?:\\* \\)?\\(.*?\\)\\(\\.\\)?$" title)
759 (format "* %s." (match-string 1 title))))))
760 (when (and dircat dirtitle)
761 (concat "@dircategory " dircat "\n"
762 "@direntry\n"
763 (let ((dirdesc
764 (let ((desc (plist-get info :texinfo-dirdesc)))
765 (cond ((not desc) nil)
766 ((org-string-match-p "\\.$" desc) desc)
767 (t (concat desc "."))))))
768 (if dirdesc (format "%-23s %s" dirtitle dirdesc) dirtitle))
769 "\n"
770 "@end direntry\n\n")))
771 ;; Title
772 "@finalout\n"
773 "@titlepage\n"
774 (format "@title %s\n" (or (plist-get info :texinfo-printed-title) title))
775 (let ((subtitle (plist-get info :subtitle)))
776 (and subtitle
777 (org-element-normalize-string
778 (replace-regexp-in-string "^" "@subtitle " subtitle))))
779 (when (plist-get info :with-author)
780 (concat
781 ;; Primary author.
782 (let ((author (org-string-nw-p
783 (org-export-data (plist-get info :author) info)))
784 (email (and (plist-get info :with-email)
785 (org-string-nw-p
786 (org-export-data (plist-get info :email) info)))))
787 (cond ((and author email)
788 (format "@author %s (@email{%s})\n" author email))
789 (author (format "@author %s\n" author))
790 (email (format "@author @email{%s}\n" email))))
791 ;; Other authors.
792 (let ((subauthor (plist-get info :subauthor)))
793 (and subauthor
794 (org-element-normalize-string
795 (replace-regexp-in-string "^" "@author " subauthor))))))
796 (and copying "@page\n@vskip 0pt plus 1filll\n@insertcopying\n")
797 "@end titlepage\n\n"
798 ;; Table of contents.
799 (and (plist-get info :with-toc) "@contents\n\n")
800 ;; Configure Top Node when not for Tex
801 "@ifnottex\n"
802 "@node Top\n"
803 (format "@top %s\n" title)
804 (and copying "@insertcopying\n")
805 "@end ifnottex\n\n"
806 ;; Menu.
807 (let ((menu (org-texinfo-make-menu info 'main))
808 (detail-menu (org-texinfo-make-menu info 'detailed)))
809 (and menu
810 (concat "@menu\n"
811 menu "\n"
812 (and detail-menu
813 (concat "\n@detailmenu\n"
814 " --- The Detailed Node Listing ---\n"
815 detail-menu "\n"
816 "@end detailmenu\n"))
817 "@end menu\n\n")))
818 ;; Document's body.
819 contents "\n"
820 ;; Creator.
821 (case (plist-get info :with-creator)
822 ((nil) nil)
823 (comment (format "@c %s\n" (plist-get info :creator)))
824 (otherwise (concat (plist-get info :creator) "\n")))
825 ;; Document end.
826 "@bye")))
830 ;;; Transcode Functions
832 ;;; Bold
834 (defun org-texinfo-bold (bold contents info)
835 "Transcode BOLD from Org to Texinfo.
836 CONTENTS is the text with bold markup. INFO is a plist holding
837 contextual information."
838 (org-texinfo--text-markup contents 'bold))
840 ;;; Center Block
842 (defun org-texinfo-center-block (center-block contents info)
843 "Transcode a CENTER-BLOCK element from Org to Texinfo.
844 CONTENTS holds the contents of the block. INFO is a plist used
845 as a communication channel."
846 contents)
848 ;;; Clock
850 (defun org-texinfo-clock (clock contents info)
851 "Transcode a CLOCK element from Org to Texinfo.
852 CONTENTS is nil. INFO is a plist holding contextual
853 information."
854 (concat
855 "@noindent"
856 (format "@strong{%s} " org-clock-string)
857 (format org-texinfo-inactive-timestamp-format
858 (concat (org-translate-time
859 (org-element-property :raw-value
860 (org-element-property :value clock)))
861 (let ((time (org-element-property :duration clock)))
862 (and time (format " (%s)" time)))))
863 "@*"))
865 ;;; Code
867 (defun org-texinfo-code (code contents info)
868 "Transcode a CODE object from Org to Texinfo.
869 CONTENTS is nil. INFO is a plist used as a communication
870 channel."
871 (org-texinfo--text-markup (org-element-property :value code) 'code))
873 ;;; Drawer
875 (defun org-texinfo-drawer (drawer contents info)
876 "Transcode a DRAWER element from Org to Texinfo.
877 CONTENTS holds the contents of the block. INFO is a plist
878 holding contextual information."
879 (let* ((name (org-element-property :drawer-name drawer))
880 (output (funcall org-texinfo-format-drawer-function
881 name contents)))
882 output))
884 ;;; Dynamic Block
886 (defun org-texinfo-dynamic-block (dynamic-block contents info)
887 "Transcode a DYNAMIC-BLOCK element from Org to Texinfo.
888 CONTENTS holds the contents of the block. INFO is a plist
889 holding contextual information. See `org-export-data'."
890 contents)
892 ;;; Entity
894 (defun org-texinfo-entity (entity contents info)
895 "Transcode an ENTITY object from Org to Texinfo.
896 CONTENTS are the definition itself. INFO is a plist holding
897 contextual information."
898 (let ((ent (org-element-property :latex entity)))
899 (if (org-element-property :latex-math-p entity) (format "@math{%s}" ent) ent)))
901 ;;; Example Block
903 (defun org-texinfo-example-block (example-block contents info)
904 "Transcode an EXAMPLE-BLOCK element from Org to Texinfo.
905 CONTENTS is nil. INFO is a plist holding contextual
906 information."
907 (format "@verbatim\n%s@end verbatim"
908 (org-export-format-code-default example-block info)))
910 ;;; Export Block
912 (defun org-texinfo-export-block (export-block contents info)
913 "Transcode a EXPORT-BLOCK element from Org to Texinfo.
914 CONTENTS is nil. INFO is a plist holding contextual information."
915 (when (string= (org-element-property :type export-block) "TEXINFO")
916 (org-remove-indentation (org-element-property :value export-block))))
918 ;;; Export Snippet
920 (defun org-texinfo-export-snippet (export-snippet contents info)
921 "Transcode a EXPORT-SNIPPET object from Org to Texinfo.
922 CONTENTS is nil. INFO is a plist holding contextual information."
923 (when (eq (org-export-snippet-backend export-snippet) 'texinfo)
924 (org-element-property :value export-snippet)))
926 ;;; Fixed Width
928 (defun org-texinfo-fixed-width (fixed-width contents info)
929 "Transcode a FIXED-WIDTH element from Org to Texinfo.
930 CONTENTS is nil. INFO is a plist holding contextual information."
931 (format "@example\n%s\n@end example"
932 (org-remove-indentation
933 (org-texinfo--sanitize-content
934 (org-element-property :value fixed-width)))))
936 ;;; Footnote Reference
939 (defun org-texinfo-footnote-reference (footnote contents info)
940 "Create a footnote reference for FOOTNOTE.
942 FOOTNOTE is the footnote to define. CONTENTS is nil. INFO is a
943 plist holding contextual information."
944 (let ((def (org-export-get-footnote-definition footnote info)))
945 (format "@footnote{%s}"
946 (org-trim (org-export-data def info)))))
948 ;;; Headline
950 (defun org-texinfo-headline (headline contents info)
951 "Transcode a HEADLINE element from Org to Texinfo.
952 CONTENTS holds the contents of the headline. INFO is a plist
953 holding contextual information."
954 (let* ((class (plist-get info :texinfo-class))
955 (level (org-export-get-relative-level headline info))
956 (numberedp (org-export-numbered-headline-p headline info))
957 (class-sectioning (assoc class org-texinfo-classes))
958 ;; Find the index type, if any
959 (index (org-element-property :INDEX headline))
960 ;; Retrieve headline text
961 (text (org-texinfo--sanitize-headline
962 (org-element-property :title headline) info))
963 ;; Create node info, to insert it before section formatting.
964 ;; Use custom menu title if present
965 (node (format "@node %s\n" (org-texinfo--get-node headline info)))
966 ;; Menus must be generated with first child, otherwise they
967 ;; will not nest properly
968 (menu (let* ((first (org-export-first-sibling-p headline info))
969 (parent (org-export-get-parent-headline headline))
970 (title (org-texinfo--sanitize-headline
971 (org-element-property :title parent) info))
972 heading listing
973 (tree (plist-get info :parse-tree)))
974 (if first
975 (org-element-map (plist-get info :parse-tree) 'headline
976 (lambda (ref)
977 (if (member title (org-element-property :title ref))
978 (push ref heading)))
979 info t))
980 (setq listing (org-texinfo--build-menu
981 (car heading) level info))
982 (if listing
983 (setq listing (replace-regexp-in-string
984 "%" "%%" listing)
985 listing (format
986 "\n@menu\n%s\n@end menu\n\n" listing))
987 'nil)))
988 ;; Section formatting will set two placeholders: one for the
989 ;; title and the other for the contents.
990 (section-fmt
991 (if (org-not-nil (org-element-property :APPENDIX headline))
992 (concat menu node "appendix\n%s")
993 (let ((sec (if (and (symbolp (nth 2 class-sectioning))
994 (fboundp (nth 2 class-sectioning)))
995 (funcall (nth 2 class-sectioning) level numberedp)
996 (nth (1+ level) class-sectioning))))
997 (cond
998 ;; No section available for that LEVEL.
999 ((not sec) nil)
1000 ;; Section format directly returned by a function.
1001 ((stringp sec) sec)
1002 ;; (numbered-section . unnumbered-section)
1003 ((not (consp (cdr sec)))
1004 (concat menu
1005 node
1006 ;; An index is always unnumbered.
1007 (if (or index (not numberedp)) (cdr sec) (car sec))
1008 "\n%s"))))))
1009 (todo
1010 (and (plist-get info :with-todo-keywords)
1011 (let ((todo (org-element-property :todo-keyword headline)))
1012 (and todo (org-export-data todo info)))))
1013 (todo-type (and todo (org-element-property :todo-type headline)))
1014 (tags (and (plist-get info :with-tags)
1015 (org-export-get-tags headline info)))
1016 (priority (and (plist-get info :with-priority)
1017 (org-element-property :priority headline)))
1018 ;; Create the headline text along with a no-tag version. The
1019 ;; latter is required to remove tags from table of contents.
1020 (full-text (org-texinfo--sanitize-content
1021 (if (not (eq org-texinfo-format-headline-function 'ignore))
1022 ;; User-defined formatting function.
1023 (funcall org-texinfo-format-headline-function
1024 todo todo-type priority text tags)
1025 ;; Default formatting.
1026 (concat
1027 (when todo
1028 (format "@strong{%s} " todo))
1029 (when priority (format "@emph{#%s} " priority))
1030 text
1031 (when tags
1032 (format " :%s:"
1033 (mapconcat 'identity tags ":")))))))
1034 (full-text-no-tag
1035 (org-texinfo--sanitize-content
1036 (if (not (eq org-texinfo-format-headline-function 'ignore))
1037 ;; User-defined formatting function.
1038 (funcall org-texinfo-format-headline-function
1039 todo todo-type priority text nil)
1040 ;; Default formatting.
1041 (concat
1042 (when todo (format "@strong{%s} " todo))
1043 (when priority (format "@emph{#%c} " priority))
1044 text))))
1045 (pre-blanks
1046 (make-string (org-element-property :pre-blank headline) 10)))
1047 (cond
1048 ;; Case 1: This is a footnote section: ignore it.
1049 ((org-element-property :footnote-section-p headline) nil)
1050 ;; Case 2: This is the `copying' section: ignore it
1051 ;; This is used elsewhere.
1052 ((org-element-property :COPYING headline) nil)
1053 ;; Case 3: An index. If it matches one of the known indexes,
1054 ;; print it as such following the contents, otherwise
1055 ;; print the contents and leave the index up to the user.
1056 (index
1057 (format
1058 section-fmt full-text
1059 (concat pre-blanks contents "\n"
1060 (if (member index '("cp" "fn" "ky" "pg" "tp" "vr"))
1061 (concat "@printindex " index)))))
1062 ;; Case 4: This is a deep sub-tree: export it as a list item.
1063 ;; Also export as items headlines for which no section
1064 ;; format has been found.
1065 ((or (not section-fmt) (org-export-low-level-p headline info))
1066 ;; Build the real contents of the sub-tree.
1067 (let ((low-level-body
1068 (concat
1069 ;; If the headline is the first sibling, start a list.
1070 (when (org-export-first-sibling-p headline info)
1071 (format "@%s\n" (if numberedp 'enumerate 'itemize)))
1072 ;; Itemize headline
1073 "@item\n" full-text "\n" pre-blanks contents)))
1074 ;; If headline is not the last sibling simply return
1075 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1076 ;; blank line.
1077 (if (not (org-export-last-sibling-p headline info)) low-level-body
1078 (replace-regexp-in-string
1079 "[ \t\n]*\\'"
1080 (format "\n@end %s" (if numberedp 'enumerate 'itemize))
1081 low-level-body))))
1082 ;; Case 5: Standard headline. Export it as a section.
1084 (cond
1085 ((not (and tags (eq (plist-get info :with-tags) 'not-in-toc)))
1086 ;; Regular section. Use specified format string.
1087 (format (replace-regexp-in-string "%]" "%%]" section-fmt) full-text
1088 (concat pre-blanks contents)))
1089 ((string-match "\\`@\\(.*?\\){" section-fmt)
1090 ;; If tags should be removed from table of contents, insert
1091 ;; title without tags as an alternative heading in sectioning
1092 ;; command.
1093 (format (replace-match (concat (match-string 1 section-fmt) "[%s]")
1094 nil nil section-fmt 1)
1095 ;; Replace square brackets with parenthesis since
1096 ;; square brackets are not supported in optional
1097 ;; arguments.
1098 (replace-regexp-in-string
1099 "\\[" "("
1100 (replace-regexp-in-string
1101 "\\]" ")"
1102 full-text-no-tag))
1103 full-text
1104 (concat pre-blanks contents)))
1106 ;; Impossible to add an alternative heading. Fallback to
1107 ;; regular sectioning format string.
1108 (format (replace-regexp-in-string "%]" "%%]" section-fmt) full-text
1109 (concat pre-blanks contents))))))))
1111 ;;; Inline Src Block
1113 (defun org-texinfo-inline-src-block (inline-src-block contents info)
1114 "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
1115 CONTENTS holds the contents of the item. INFO is a plist holding
1116 contextual information."
1117 (let* ((code (org-element-property :value inline-src-block))
1118 (separator (org-texinfo--find-verb-separator code)))
1119 (concat "@verb{" separator code separator "}")))
1121 ;;; Inlinetask
1123 (defun org-texinfo-inlinetask (inlinetask contents info)
1124 "Transcode an INLINETASK element from Org to Texinfo.
1125 CONTENTS holds the contents of the block. INFO is a plist
1126 holding contextual information."
1127 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1128 (todo (and (plist-get info :with-todo-keywords)
1129 (let ((todo (org-element-property :todo-keyword inlinetask)))
1130 (and todo (org-export-data todo info)))))
1131 (todo-type (org-element-property :todo-type inlinetask))
1132 (tags (and (plist-get info :with-tags)
1133 (org-export-get-tags inlinetask info)))
1134 (priority (and (plist-get info :with-priority)
1135 (org-element-property :priority inlinetask))))
1136 ;; If `org-texinfo-format-inlinetask-function' is provided, call it
1137 ;; with appropriate arguments.
1138 (if (not (eq org-texinfo-format-inlinetask-function 'ignore))
1139 (funcall org-texinfo-format-inlinetask-function
1140 todo todo-type priority title tags contents)
1141 ;; Otherwise, use a default template.
1142 (let ((full-title
1143 (concat
1144 (when todo (format "@strong{%s} " todo))
1145 (when priority (format "#%c " priority))
1146 title
1147 (when tags (format ":%s:"
1148 (mapconcat 'identity tags ":"))))))
1149 (format (concat "@center %s\n\n"
1150 "%s"
1151 "\n")
1152 full-title contents)))))
1154 ;;; Italic
1156 (defun org-texinfo-italic (italic contents info)
1157 "Transcode ITALIC from Org to Texinfo.
1158 CONTENTS is the text with italic markup. INFO is a plist holding
1159 contextual information."
1160 (org-texinfo--text-markup contents 'italic))
1162 ;;; Item
1164 (defun org-texinfo-item (item contents info)
1165 "Transcode an ITEM element from Org to Texinfo.
1166 CONTENTS holds the contents of the item. INFO is a plist holding
1167 contextual information."
1168 (let* ((tag (org-element-property :tag item))
1169 (desc (org-export-data tag info)))
1170 (concat "\n@item " (if tag desc) "\n"
1171 (and contents (org-trim contents)) "\n")))
1173 ;;; Keyword
1175 (defun org-texinfo-keyword (keyword contents info)
1176 "Transcode a KEYWORD element from Org to Texinfo.
1177 CONTENTS is nil. INFO is a plist holding contextual information."
1178 (let ((key (org-element-property :key keyword))
1179 (value (org-element-property :value keyword)))
1180 (cond
1181 ((string= key "TEXINFO") value)
1182 ((string= key "CINDEX") (format "@cindex %s" value))
1183 ((string= key "FINDEX") (format "@findex %s" value))
1184 ((string= key "KINDEX") (format "@kindex %s" value))
1185 ((string= key "PINDEX") (format "@pindex %s" value))
1186 ((string= key "TINDEX") (format "@tindex %s" value))
1187 ((string= key "VINDEX") (format "@vindex %s" value)))))
1189 ;;; Line Break
1191 (defun org-texinfo-line-break (line-break contents info)
1192 "Transcode a LINE-BREAK object from Org to Texinfo.
1193 CONTENTS is nil. INFO is a plist holding contextual information."
1194 "@*\n")
1196 ;;; Link
1198 (defun org-texinfo-link (link desc info)
1199 "Transcode a LINK object from Org to Texinfo.
1201 DESC is the description part of the link, or the empty string.
1202 INFO is a plist holding contextual information. See
1203 `org-export-data'."
1204 (let* ((type (org-element-property :type link))
1205 (raw-path (org-element-property :path link))
1206 ;; Ensure DESC really exists, or set it to nil.
1207 (desc (and (not (string= desc "")) desc))
1208 (path (cond
1209 ((member type '("http" "https" "ftp"))
1210 (concat type ":" raw-path))
1211 ((and (string= type "file") (file-name-absolute-p raw-path))
1212 (concat "file:" raw-path))
1213 (t raw-path)))
1214 (email (if (string= type "mailto")
1215 (let ((text (replace-regexp-in-string
1216 "@" "@@" raw-path)))
1217 (concat text (if desc (concat "," desc))))))
1218 protocol)
1219 (cond
1220 ;; Links pointing to a headline: Find destination and build
1221 ;; appropriate referencing command.
1222 ((member type '("custom-id" "id"))
1223 (let ((destination (org-export-resolve-id-link link info)))
1224 (case (org-element-type destination)
1225 ;; Id link points to an external file.
1226 (plain-text
1227 (if desc (format "@uref{file://%s,%s}" destination desc)
1228 (format "@uref{file://%s}" destination)))
1229 ;; LINK points to a headline. Use the headline as the NODE target
1230 (headline
1231 (format "@ref{%s,%s}"
1232 (org-texinfo--get-node destination info)
1233 (or desc "")))
1234 (otherwise
1235 (let ((path (org-export-solidify-link-text path)))
1236 (if (not desc) (format "@ref{%s}" path)
1237 (format "@ref{%s,,%s}" path desc)))))))
1238 ((member type '("info"))
1239 (let* ((info-path (split-string path "[:#]"))
1240 (info-manual (car info-path))
1241 (info-node (or (cadr info-path) "top"))
1242 (title (or desc "")))
1243 (format "@ref{%s,%s,,%s,}" info-node title info-manual)))
1244 ((member type '("fuzzy"))
1245 (let ((destination (org-export-resolve-fuzzy-link link info)))
1246 (case (org-element-type destination)
1247 ;; Id link points to an external file.
1248 (plain-text
1249 (if desc (format "@uref{file://%s,%s}" destination desc)
1250 (format "@uref{file://%s}" destination)))
1251 ;; LINK points to a headline. Use the headline as the NODE target
1252 (headline
1253 (format "@ref{%s,%s}"
1254 (org-texinfo--get-node destination info)
1255 (or desc "")))
1256 (otherwise
1257 (let ((path (org-export-solidify-link-text path)))
1258 (if (not desc) (format "@ref{%s}" path)
1259 (format "@ref{%s,,%s}" path desc)))))))
1260 ;; Special case for email addresses
1261 (email
1262 (format "@email{%s}" email))
1263 ;; External link with a description part.
1264 ((and path desc) (format "@uref{%s,%s}" path desc))
1265 ;; External link without a description part.
1266 (path (format "@uref{%s}" path))
1267 ;; No path, only description. Try to do something useful.
1268 (t (format org-texinfo-link-with-unknown-path-format desc)))))
1271 ;;; Menu
1273 (defun org-texinfo-make-menu (info level)
1274 "Create the menu for inclusion in the texifo document.
1276 INFO is the parsed buffer that contains the headlines. LEVEL
1277 determines whether to make the main menu, or the detailed menu.
1279 This is only used for generating the primary menu. In-Node menus
1280 are generated directly."
1281 (let ((parse (plist-get info :parse-tree)))
1282 (cond
1283 ;; Generate the main menu
1284 ((eq level 'main) (org-texinfo--build-menu parse 1 info))
1285 ;; Generate the detailed (recursive) menu
1286 ((eq level 'detailed)
1287 ;; Requires recursion
1288 ;;(org-texinfo--build-detailed-menu parse top info)
1289 (org-texinfo--build-menu parse 1 info 'detailed)))))
1291 ;;; Paragraph
1293 (defun org-texinfo-paragraph (paragraph contents info)
1294 "Transcode a PARAGRAPH element from Org to Texinfo.
1295 CONTENTS is the contents of the paragraph, as a string. INFO is
1296 the plist used as a communication channel."
1297 contents)
1299 ;;; Plain List
1301 (defun org-texinfo-plain-list (plain-list contents info)
1302 "Transcode a PLAIN-LIST element from Org to Texinfo.
1303 CONTENTS is the contents of the list. INFO is a plist holding
1304 contextual information."
1305 (let* ((attr (org-export-read-attribute :attr_texinfo plain-list))
1306 (indic (or (plist-get attr :indic)
1307 org-texinfo-def-table-markup))
1308 (type (org-element-property :type plain-list))
1309 (table-type (plist-get attr :table-type))
1310 ;; Ensure valid texinfo table type.
1311 (table-type (if (member table-type '("ftable" "vtable")) table-type
1312 "table"))
1313 (list-type (cond
1314 ((eq type 'ordered) "enumerate")
1315 ((eq type 'unordered) "itemize")
1316 ((eq type 'descriptive) table-type))))
1317 (format "@%s%s\n@end %s"
1318 (if (eq type 'descriptive)
1319 (concat list-type " " indic)
1320 list-type)
1321 contents
1322 list-type)))
1324 ;;; Plain Text
1326 (defun org-texinfo-plain-text (text info)
1327 "Transcode a TEXT string from Org to Texinfo.
1328 TEXT is the string to transcode. INFO is a plist holding
1329 contextual information."
1330 ;; First protect @, { and }.
1331 (let ((output (org-texinfo--sanitize-content text)))
1332 ;; Activate smart quotes. Be sure to provide original TEXT string
1333 ;; since OUTPUT may have been modified.
1334 (when (plist-get info :with-smart-quotes)
1335 (setq output
1336 (org-export-activate-smart-quotes output :texinfo info text)))
1337 ;; LaTeX into @LaTeX{} and TeX into @TeX{}
1338 (let ((case-fold-search nil)
1339 (start 0))
1340 (while (string-match "\\(\\(?:La\\)?TeX\\)" output start)
1341 (setq output (replace-match
1342 (format "@%s{}" (match-string 1 output)) nil t output)
1343 start (match-end 0))))
1344 ;; Convert special strings.
1345 (when (plist-get info :with-special-strings)
1346 (while (string-match (regexp-quote "...") output)
1347 (setq output (replace-match "@dots{}" nil t output))))
1348 ;; Handle break preservation if required.
1349 (when (plist-get info :preserve-breaks)
1350 (setq output (replace-regexp-in-string
1351 "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" output)))
1352 ;; Return value.
1353 output))
1355 ;;; Planning
1357 (defun org-texinfo-planning (planning contents info)
1358 "Transcode a PLANNING element from Org to Texinfo.
1359 CONTENTS is nil. INFO is a plist holding contextual
1360 information."
1361 (concat
1362 "@noindent"
1363 (mapconcat
1364 'identity
1365 (delq nil
1366 (list
1367 (let ((closed (org-element-property :closed planning)))
1368 (when closed
1369 (concat
1370 (format "@strong{%s} " org-closed-string)
1371 (format org-texinfo-inactive-timestamp-format
1372 (org-translate-time
1373 (org-element-property :raw-value closed))))))
1374 (let ((deadline (org-element-property :deadline planning)))
1375 (when deadline
1376 (concat
1377 (format "@strong{%s} " org-deadline-string)
1378 (format org-texinfo-active-timestamp-format
1379 (org-translate-time
1380 (org-element-property :raw-value deadline))))))
1381 (let ((scheduled (org-element-property :scheduled planning)))
1382 (when scheduled
1383 (concat
1384 (format "@strong{%s} " org-scheduled-string)
1385 (format org-texinfo-active-timestamp-format
1386 (org-translate-time
1387 (org-element-property :raw-value scheduled))))))))
1388 " ")
1389 "@*"))
1391 ;;; Property Drawer
1393 (defun org-texinfo-property-drawer (property-drawer contents info)
1394 "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
1395 CONTENTS is nil. INFO is a plist holding contextual
1396 information."
1397 ;; The property drawer isn't exported but we want separating blank
1398 ;; lines nonetheless.
1401 ;;; Quote Block
1403 (defun org-texinfo-quote-block (quote-block contents info)
1404 "Transcode a QUOTE-BLOCK element from Org to Texinfo.
1405 CONTENTS holds the contents of the block. INFO is a plist
1406 holding contextual information."
1407 (let* ((title (org-element-property :name quote-block))
1408 (start-quote (concat "@quotation"
1409 (if title
1410 (format " %s" title)))))
1411 (format "%s\n%s@end quotation" start-quote contents)))
1413 ;;; Quote Section
1415 (defun org-texinfo-quote-section (quote-section contents info)
1416 "Transcode a QUOTE-SECTION element from Org to Texinfo.
1417 CONTENTS is nil. INFO is a plist holding contextual information."
1418 (let ((value (org-remove-indentation
1419 (org-element-property :value quote-section))))
1420 (when value (format "@verbatim\n%s@end verbatim" value))))
1422 ;;; Radio Target
1424 (defun org-texinfo-radio-target (radio-target text info)
1425 "Transcode a RADIO-TARGET object from Org to Texinfo.
1426 TEXT is the text of the target. INFO is a plist holding
1427 contextual information."
1428 (format "@anchor{%s}%s"
1429 (org-export-solidify-link-text
1430 (org-element-property :value radio-target))
1431 text))
1433 ;;; Section
1435 (defun org-texinfo-section (section contents info)
1436 "Transcode a SECTION element from Org to Texinfo.
1437 CONTENTS holds the contents of the section. INFO is a plist
1438 holding contextual information."
1439 contents)
1441 ;;; Special Block
1443 (defun org-texinfo-special-block (special-block contents info)
1444 "Transcode a SPECIAL-BLOCK element from Org to Texinfo.
1445 CONTENTS holds the contents of the block. INFO is a plist used
1446 as a communication channel."
1447 contents)
1449 ;;; Src Block
1451 (defun org-texinfo-src-block (src-block contents info)
1452 "Transcode a SRC-BLOCK element from Org to Texinfo.
1453 CONTENTS holds the contents of the item. INFO is a plist holding
1454 contextual information."
1455 (let* ((lang (org-element-property :language src-block))
1456 (lisp-p (string-match-p "lisp" lang))
1457 (src-contents (org-texinfo--sanitize-content
1458 (org-export-format-code-default src-block info))))
1459 (cond
1460 ;; Case 1. Lisp Block
1461 (lisp-p
1462 (format "@lisp\n%s@end lisp"
1463 src-contents))
1464 ;; Case 2. Other blocks
1466 (format "@example\n%s@end example"
1467 src-contents)))))
1469 ;;; Statistics Cookie
1471 (defun org-texinfo-statistics-cookie (statistics-cookie contents info)
1472 "Transcode a STATISTICS-COOKIE object from Org to Texinfo.
1473 CONTENTS is nil. INFO is a plist holding contextual information."
1474 (org-element-property :value statistics-cookie))
1476 ;;; Subscript
1478 (defun org-texinfo-subscript (subscript contents info)
1479 "Transcode a SUBSCRIPT object from Org to Texinfo.
1480 CONTENTS is the contents of the object. INFO is a plist holding
1481 contextual information."
1482 (format "@math{_%s}" contents))
1484 ;;; Superscript
1486 (defun org-texinfo-superscript (superscript contents info)
1487 "Transcode a SUPERSCRIPT 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 ;;; Table
1494 (defun org-texinfo-table (table contents info)
1495 "Transcode a TABLE element from Org to Texinfo.
1496 CONTENTS is the contents of the table. INFO is a plist holding
1497 contextual information."
1498 (if (eq (org-element-property :type table) 'table.el)
1499 (format "@verbatim\n%s@end verbatim"
1500 (org-element-normalize-string
1501 (org-element-property :value table)))
1502 (let* ((col-width (org-export-read-attribute :attr_texinfo table :columns))
1503 (columns
1504 (if col-width (format "@columnfractions %s" col-width)
1505 (org-texinfo-table-column-widths table info))))
1506 (format "@multitable %s\n%s@end multitable"
1507 columns
1508 contents))))
1510 (defun org-texinfo-table-column-widths (table info)
1511 "Determine the largest table cell in each column to process alignment.
1512 TABLE is the table element to transcode. INFO is a plist used as
1513 a communication channel."
1514 (let ((widths (make-vector (cdr (org-export-table-dimensions table info)) 0)))
1515 (org-element-map table 'table-row
1516 (lambda (row)
1517 (let ((idx 0))
1518 (org-element-map row 'table-cell
1519 (lambda (cell)
1520 ;; Length of the cell in the original buffer is only an
1521 ;; approximation of the length of the cell in the
1522 ;; output. It can sometimes fail (e.g. it considers
1523 ;; "/a/" being larger than "ab").
1524 (let ((w (- (org-element-property :contents-end cell)
1525 (org-element-property :contents-begin cell))))
1526 (aset widths idx (max w (aref widths idx))))
1527 (incf idx))
1528 info)))
1529 info)
1530 (format "{%s}" (mapconcat (lambda (w) (make-string w ?a)) widths "} {"))))
1532 ;;; Table Cell
1534 (defun org-texinfo-table-cell (table-cell contents info)
1535 "Transcode a TABLE-CELL element from Org to Texinfo.
1536 CONTENTS is the cell contents. INFO is a plist used as
1537 a communication channel."
1538 (concat (if (and contents
1539 org-texinfo-table-scientific-notation
1540 (string-match orgtbl-exp-regexp contents))
1541 ;; Use appropriate format string for scientific
1542 ;; notation.
1543 (format org-texinfo-table-scientific-notation
1544 (match-string 1 contents)
1545 (match-string 2 contents))
1546 contents)
1547 (when (org-export-get-next-element table-cell info) "\n@tab ")))
1549 ;;; Table Row
1551 (defun org-texinfo-table-row (table-row contents info)
1552 "Transcode a TABLE-ROW element from Org to Texinfo.
1553 CONTENTS is the contents of the row. INFO is a plist used as
1554 a communication channel."
1555 ;; Rules are ignored since table separators are deduced from
1556 ;; borders of the current row.
1557 (when (eq (org-element-property :type table-row) 'standard)
1558 (let ((rowgroup-tag
1559 (if (and (= 1 (org-export-table-row-group table-row info))
1560 (org-export-table-has-header-p
1561 (org-export-get-parent-table table-row) info))
1562 "@headitem "
1563 "@item ")))
1564 (concat rowgroup-tag contents "\n"))))
1566 ;;; Target
1568 (defun org-texinfo-target (target contents info)
1569 "Transcode a TARGET object from Org to Texinfo.
1570 CONTENTS is nil. INFO is a plist holding contextual
1571 information."
1572 (format "@anchor{%s}"
1573 (org-export-solidify-link-text (org-element-property :value target))))
1575 ;;; Timestamp
1577 (defun org-texinfo-timestamp (timestamp contents info)
1578 "Transcode a TIMESTAMP object from Org to Texinfo.
1579 CONTENTS is nil. INFO is a plist holding contextual
1580 information."
1581 (let ((value (org-texinfo-plain-text
1582 (org-timestamp-translate timestamp) info)))
1583 (case (org-element-property :type timestamp)
1584 ((active active-range)
1585 (format org-texinfo-active-timestamp-format value))
1586 ((inactive inactive-range)
1587 (format org-texinfo-inactive-timestamp-format value))
1588 (t (format org-texinfo-diary-timestamp-format value)))))
1590 ;;; Verbatim
1592 (defun org-texinfo-verbatim (verbatim contents info)
1593 "Transcode a VERBATIM object from Org to Texinfo.
1594 CONTENTS is nil. INFO is a plist used as a communication
1595 channel."
1596 (org-texinfo--text-markup (org-element-property :value verbatim) 'verbatim))
1598 ;;; Verse Block
1600 (defun org-texinfo-verse-block (verse-block contents info)
1601 "Transcode a VERSE-BLOCK element from Org to Texinfo.
1602 CONTENTS is verse block contents. INFO is a plist holding
1603 contextual information."
1604 (format "@display\n%s@end display" contents))
1607 ;;; Interactive functions
1609 (defun org-texinfo-export-to-texinfo
1610 (&optional async subtreep visible-only body-only ext-plist)
1611 "Export current buffer to a Texinfo file.
1613 If narrowing is active in the current buffer, only export its
1614 narrowed part.
1616 If a region is active, export that region.
1618 A non-nil optional argument ASYNC means the process should happen
1619 asynchronously. The resulting file should be accessible through
1620 the `org-export-stack' interface.
1622 When optional argument SUBTREEP is non-nil, export the sub-tree
1623 at point, extracting information from the headline properties
1624 first.
1626 When optional argument VISIBLE-ONLY is non-nil, don't export
1627 contents of hidden elements.
1629 When optional argument BODY-ONLY is non-nil, only write code
1630 between \"\\begin{document}\" and \"\\end{document}\".
1632 EXT-PLIST, when provided, is a property list with external
1633 parameters overriding Org default settings, but still inferior to
1634 file-local settings.
1636 Return output file's name."
1637 (interactive)
1638 (let ((outfile (org-export-output-file-name ".texi" subtreep))
1639 (org-export-coding-system `,org-texinfo-coding-system))
1640 (org-export-to-file 'texinfo outfile
1641 async subtreep visible-only body-only ext-plist)))
1643 (defun org-texinfo-export-to-info
1644 (&optional async subtreep visible-only body-only ext-plist)
1645 "Export current buffer to Texinfo then process through to INFO.
1647 If narrowing is active in the current buffer, only export its
1648 narrowed part.
1650 If a region is active, export that region.
1652 A non-nil optional argument ASYNC means the process should happen
1653 asynchronously. The resulting file should be accessible through
1654 the `org-export-stack' interface.
1656 When optional argument SUBTREEP is non-nil, export the sub-tree
1657 at point, extracting information from the headline properties
1658 first.
1660 When optional argument VISIBLE-ONLY is non-nil, don't export
1661 contents of hidden elements.
1663 When optional argument BODY-ONLY is non-nil, only write code
1664 between \"\\begin{document}\" and \"\\end{document}\".
1666 EXT-PLIST, when provided, is a property list with external
1667 parameters overriding Org default settings, but still inferior to
1668 file-local settings.
1670 When optional argument PUB-DIR is set, use it as the publishing
1671 directory.
1673 Return INFO file's name."
1674 (interactive)
1675 (let ((outfile (org-export-output-file-name ".texi" subtreep))
1676 (org-export-coding-system `,org-texinfo-coding-system))
1677 (org-export-to-file 'texinfo outfile
1678 async subtreep visible-only body-only ext-plist
1679 (lambda (file) (org-texinfo-compile file)))))
1681 ;;;###autoload
1682 (defun org-texinfo-publish-to-texinfo (plist filename pub-dir)
1683 "Publish an org file to Texinfo.
1685 FILENAME is the filename of the Org file to be published. PLIST
1686 is the property list for the given project. PUB-DIR is the
1687 publishing directory.
1689 Return output file name."
1690 (org-publish-org-to 'texinfo filename ".texi" plist pub-dir))
1692 ;;;###autoload
1693 (defun org-texinfo-convert-region-to-texinfo ()
1694 "Assume the current region has org-mode syntax, and convert it to Texinfo.
1695 This can be used in any buffer. For example, you can write an
1696 itemized list in org-mode syntax in an Texinfo buffer and use
1697 this command to convert it."
1698 (interactive)
1699 (org-export-replace-region-by 'texinfo))
1701 (defun org-texinfo-compile (file)
1702 "Compile a texinfo file.
1704 FILE is the name of the file being compiled. Processing is
1705 done through the command specified in `org-texinfo-info-process'.
1707 Return INFO file name or an error if it couldn't be produced."
1708 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1709 (full-name (file-truename file))
1710 (out-dir (file-name-directory file))
1711 ;; Properly set working directory for compilation.
1712 (default-directory (if (file-name-absolute-p file)
1713 (file-name-directory full-name)
1714 default-directory))
1715 errors)
1716 (message (format "Processing Texinfo file %s..." file))
1717 (save-window-excursion
1718 (cond
1719 ;; A function is provided: Apply it.
1720 ((functionp org-texinfo-info-process)
1721 (funcall org-texinfo-info-process (shell-quote-argument file)))
1722 ;; A list is provided: Replace %b, %f and %o with appropriate
1723 ;; values in each command before applying it. Output is
1724 ;; redirected to "*Org INFO Texinfo Output*" buffer.
1725 ((consp org-texinfo-info-process)
1726 (let ((outbuf (get-buffer-create "*Org INFO Texinfo Output*")))
1727 (mapc
1728 (lambda (command)
1729 (shell-command
1730 (replace-regexp-in-string
1731 "%b" (shell-quote-argument base-name)
1732 (replace-regexp-in-string
1733 "%f" (shell-quote-argument full-name)
1734 (replace-regexp-in-string
1735 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1736 outbuf))
1737 org-texinfo-info-process)
1738 ;; Collect standard errors from output buffer.
1739 (setq errors (org-texinfo-collect-errors outbuf))))
1740 (t (error "No valid command to process to Info")))
1741 (let ((infofile (concat out-dir base-name ".info")))
1742 ;; Check for process failure. Provide collected errors if
1743 ;; possible.
1744 (if (not (file-exists-p infofile))
1745 (error (concat (format "INFO file %s wasn't produced" infofile)
1746 (when errors (concat ": " errors))))
1747 ;; Else remove log files, when specified, and signal end of
1748 ;; process to user, along with any error encountered.
1749 (when org-texinfo-remove-logfiles
1750 (dolist (ext org-texinfo-logfiles-extensions)
1751 (let ((file (concat out-dir base-name "." ext)))
1752 (when (file-exists-p file) (delete-file file)))))
1753 (message (concat "Process completed"
1754 (if (not errors) "."
1755 (concat " with errors: " errors)))))
1756 ;; Return output file name.
1757 infofile))))
1759 (defun org-texinfo-collect-errors (buffer)
1760 "Collect some kind of errors from \"makeinfo\" command output.
1762 BUFFER is the buffer containing output.
1764 Return collected error types as a string, or nil if there was
1765 none."
1766 (with-current-buffer buffer
1767 (save-excursion
1768 (goto-char (point-min))
1769 ;; Find final "makeinfo" run.
1770 (when t
1771 (let ((case-fold-search t)
1772 (errors ""))
1773 (when (save-excursion
1774 (re-search-forward "perhaps incorrect sectioning?" nil t))
1775 (setq errors (concat errors " [incorrect sectioning]")))
1776 (when (save-excursion
1777 (re-search-forward "missing close brace" nil t))
1778 (setq errors (concat errors " [syntax error]")))
1779 (when (save-excursion
1780 (re-search-forward "Unknown command" nil t))
1781 (setq errors (concat errors " [undefined @command]")))
1782 (when (save-excursion
1783 (re-search-forward "No matching @end" nil t))
1784 (setq errors (concat errors " [block incomplete]")))
1785 (when (save-excursion
1786 (re-search-forward "requires a sectioning" nil t))
1787 (setq errors (concat errors " [invalid section command]")))
1788 (when (save-excursion
1789 (re-search-forward "\\[unexpected\]" nil t))
1790 (setq errors (concat errors " [unexpected error]")))
1791 (when (save-excursion
1792 (re-search-forward "misplaced " nil t))
1793 (setq errors (concat errors " [syntax error]")))
1794 (and (org-string-nw-p errors) (org-trim errors)))))))
1797 (provide 'ox-texinfo)
1799 ;; Local variables:
1800 ;; generated-autoload-file: "org-loaddefs.el"
1801 ;; End:
1803 ;;; ox-texinfo.el ends here