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