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