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