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