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