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