ox-texinfo.el: Add support for # in info links
[org-mode.git] / lisp / ox-texinfo.el
blobbc24694edfb7bcb940bc79d0f883cf32fa8efd7d
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)
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 (defun org-texinfo--get-node (headline info)
434 "Return node entry associated to HEADLINE.
435 INFO is a plist used as a communication channel."
436 (let ((menu-title (org-element-property :TEXINFO_MENU_TITLE headline)))
437 (org-texinfo--sanitize-menu
438 (replace-regexp-in-string
439 "%" "%%"
440 (if menu-title (org-export-data menu-title info)
441 (org-texinfo--sanitize-headline
442 (org-element-property :title headline) info))))))
444 ;;; Headline sanitizing
446 (defun org-texinfo--sanitize-headline (headline info)
447 "Remove all formatting from the text of a headline for use in
448 node and menu listing."
449 (mapconcat 'identity
450 (org-texinfo--sanitize-headline-contents headline info) " "))
452 (defun org-texinfo--sanitize-headline-contents (headline info)
453 "Retrieve the content of the headline.
455 Any content that can contain further formatting is checked
456 recursively, to ensure that nested content is also properly
457 retrieved."
458 (loop for contents in headline append
459 (cond
460 ;; already a string
461 ((stringp contents)
462 (list (replace-regexp-in-string " $" "" contents)))
463 ;; Is exported as-is (value)
464 ((org-element-map contents '(verbatim code)
465 (lambda (value) (org-element-property :value value)) info))
466 ;; Has content and recurse into the content
467 ((org-element-contents contents)
468 (org-texinfo--sanitize-headline-contents
469 (org-element-contents contents) info)))))
471 ;;; Menu sanitizing
473 (defun org-texinfo--sanitize-menu (title)
474 "Remove invalid characters from TITLE for use in menus and
475 nodes.
477 Based on TEXINFO specifications, the following must be removed:
478 @ { } ( ) : . ,"
479 (replace-regexp-in-string "[@{}():,.]" "" title))
481 ;;; Content sanitizing
483 (defun org-texinfo--sanitize-content (text)
484 "Ensure characters are properly escaped when used in headlines or blocks.
486 Escape characters are: @ { }"
487 (replace-regexp-in-string "\\\([@{}]\\\)" "@\\1" text))
489 ;;; Menu creation
491 (defun org-texinfo--build-menu (tree level info &optional detailed)
492 "Create the @menu/@end menu information from TREE at headline
493 level LEVEL.
495 TREE contains the parse-tree to work with, either of the entire
496 document or of a specific parent headline. LEVEL indicates what
497 level of headlines to look at when generating the menu. INFO is
498 a plist containing contextual information.
500 Detailed determines whether to build a single level of menu, or
501 recurse into all children as well."
502 (let ((menu (org-texinfo--generate-menu-list tree level info))
503 output text-menu)
504 (cond
505 (detailed
506 ;; Looping is done within the menu generation.
507 (setq text-menu (org-texinfo--generate-detailed menu level info)))
509 (setq text-menu (org-texinfo--generate-menu-items menu info))))
510 (when text-menu
511 (setq output (org-texinfo--format-menu text-menu))
512 (mapconcat 'identity output "\n"))))
514 (defun org-texinfo--generate-detailed (menu level info)
515 "Generate a detailed listing of all subheadings within MENU starting at LEVEL.
517 MENU is the parse-tree to work with. LEVEL is the starting level
518 for the menu headlines and from which recursion occurs. INFO is
519 a plist containing contextual information."
520 (when level
521 (let ((max-depth (min org-texinfo-max-toc-depth
522 (plist-get info :headline-levels))))
523 (when (> max-depth level)
524 (loop for headline in menu append
525 (let* ((title (org-texinfo--menu-headlines headline info))
526 ;; Create list of menu entries for the next level
527 (sublist (org-texinfo--generate-menu-list
528 headline (1+ level) info))
529 ;; Generate the menu items for that level. If
530 ;; there are none omit that heading completely,
531 ;; otherwise join the title to it's related entries.
532 (submenu (if (org-texinfo--generate-menu-items sublist info)
533 (append (list title)
534 (org-texinfo--generate-menu-items sublist info))
535 'nil))
536 ;; Start the process over the next level down.
537 (recursion (org-texinfo--generate-detailed sublist (1+ level) info)))
538 (setq recursion (append submenu recursion))
539 recursion))))))
541 (defun org-texinfo--generate-menu-list (tree level info)
542 "Generate the list of headlines that are within a given level
543 of the tree for further formatting.
545 TREE is the parse-tree containing the headlines. LEVEL is the
546 headline level to generate a list of. INFO is a plist holding
547 contextual information."
548 (org-element-map tree 'headline
549 (lambda (head)
550 (and (= (org-export-get-relative-level head info) level)
551 ;; Do not take note of footnotes or copying headlines.
552 (not (org-element-property :COPYING head))
553 (not (org-element-property :footnote-section-p head))
554 ;; Collect headline.
555 head))
556 info))
558 (defun org-texinfo--generate-menu-items (items info)
559 "Generate a list of headline information from the listing ITEMS.
561 ITEMS is a list of the headlines to be converted into entries.
562 INFO is a plist containing contextual information.
564 Returns a list containing the following information from each
565 headline: length, title, description. This is used to format the
566 menu using `org-texinfo--format-menu'."
567 (loop for headline in items collect
568 (let* ((menu-title (org-texinfo--sanitize-menu
569 (org-export-data
570 (org-element-property :TEXINFO_MENU_TITLE headline)
571 info)))
572 (title (org-texinfo--sanitize-menu
573 (org-texinfo--sanitize-headline
574 (org-element-property :title headline) info)))
575 (descr (org-export-data
576 (org-element-property :DESCRIPTION headline)
577 info))
578 (menu-entry (if (string= "" menu-title) title menu-title))
579 (len (length menu-entry))
580 (output (list len menu-entry descr)))
581 output)))
583 (defun org-texinfo--menu-headlines (headline info)
584 "Retrieve the title from HEADLINE.
586 INFO is a plist holding contextual information.
588 Return the headline as a list of (length title description) with
589 length of -1 and nil description. This is used in
590 `org-texinfo--format-menu' to identify headlines as opposed to
591 entries."
592 (let ((title (org-export-data
593 (org-element-property :title headline) info)))
594 (list -1 title 'nil)))
596 (defun org-texinfo--format-menu (text-menu)
597 "Format the TEXT-MENU items to be properly printed in the menu.
599 Each entry in the menu should be provided as (length title
600 description).
602 Headlines in the detailed menu are given length -1 to ensure they
603 are never confused with other entries. They also have no
604 description.
606 Other menu items are output as:
607 Title:: description
609 With the spacing between :: and description based on the length
610 of the longest menu entry."
612 (let* ((lengths (mapcar 'car text-menu))
613 (max-length (apply 'max lengths))
614 output)
615 (setq output
616 (mapcar (lambda (name)
617 (let* ((title (nth 1 name))
618 (desc (nth 2 name))
619 (length (nth 0 name)))
620 (if (> length -1)
621 (concat "* " title ":: "
622 (make-string
623 (- (+ 3 max-length) length)
624 ?\s)
625 (if desc
626 (concat desc)))
627 (concat "\n" title "\n"))))
628 text-menu))
629 output))
631 ;;; Template
633 (defun org-texinfo-template (contents info)
634 "Return complete document string after Texinfo conversion.
635 CONTENTS is the transcoded contents string. INFO is a plist
636 holding export options."
637 (let* ((title (org-export-data (plist-get info :title) info))
638 (info-filename (or (plist-get info :texinfo-filename)
639 (file-name-nondirectory
640 (org-export-output-file-name ".info"))))
641 (author (org-export-data (plist-get info :author) info))
642 (texinfo-header (plist-get info :texinfo-header))
643 (texinfo-post-header (plist-get info :texinfo-post-header))
644 (subtitle (plist-get info :subtitle))
645 (subauthor (plist-get info :subauthor))
646 (class (plist-get info :texinfo-class))
647 (header (nth 1 (assoc class org-texinfo-classes)))
648 (copying
649 (org-element-map (plist-get info :parse-tree) 'headline
650 (lambda (hl) (and (org-element-property :COPYING hl) hl)) info t))
651 (dircat (plist-get info :texinfo-dircat))
652 (dirtitle (plist-get info :texinfo-dirtitle))
653 (dirdesc (plist-get info :texinfo-dirdesc))
654 ;; Spacing to align description (column 32 - 3 for `* ' and
655 ;; `.' in text.
656 (dirspacing (- 29 (length dirtitle)))
657 (menu (org-texinfo-make-menu info 'main))
658 (detail-menu (org-texinfo-make-menu info 'detailed)))
659 (concat
660 ;; Header
661 header "\n"
662 "@c %**start of header\n"
663 ;; Filename and Title
664 "@setfilename " info-filename "\n"
665 "@settitle " title "\n"
666 "\n\n"
667 "@c Version and Contact Info\n"
668 "@set AUTHOR " author "\n"
670 ;; Additional Header Options set by `#+TEXINFO_HEADER
671 (if texinfo-header
672 (concat "\n"
673 texinfo-header
674 "\n"))
676 "@c %**end of header\n"
677 "@finalout\n"
678 "\n\n"
680 ;; Additional Header Options set by #+TEXINFO_POST_HEADER
681 (if texinfo-post-header
682 (concat "\n"
683 texinfo-post-header
684 "\n"))
686 ;; Copying
687 "@copying\n"
688 ;; Only export the content of the headline, do not need the
689 ;; initial headline.
690 (org-export-data (nth 2 copying) info)
691 "@end copying\n"
692 "\n\n"
694 ;; Info directory information
695 ;; Only supply if both title and category are provided
696 (if (and dircat dirtitle)
697 (concat "@dircategory " dircat "\n"
698 "@direntry\n"
699 "* " dirtitle "."
700 (make-string dirspacing ?\s)
701 dirdesc "\n"
702 "@end direntry\n"))
703 "\n\n"
705 ;; Title
706 "@titlepage\n"
707 "@title " title "\n\n"
708 (if subtitle
709 (concat "@subtitle " subtitle "\n"))
710 "@author " author "\n"
711 (if subauthor
712 (concat subauthor "\n"))
713 "\n"
714 "@c The following two commands start the copyright page.\n"
715 "@page\n"
716 "@vskip 0pt plus 1filll\n"
717 "@insertcopying\n"
718 "@end titlepage\n\n"
719 "@c Output the table of contents at the beginning.\n"
720 "@contents\n\n"
722 ;; Configure Top Node when not for Tex
723 "@ifnottex\n"
724 "@node Top\n"
725 "@top " title " Manual\n"
726 "@insertcopying\n"
727 "@end ifnottex\n\n"
729 ;; Do not output menus if they are empty
730 (if menu
731 ;; Menu
732 (concat "@menu\n"
733 menu
734 "\n\n"
735 ;; Detailed Menu
736 (if detail-menu
737 (concat "@detailmenu\n"
738 " --- The Detailed Node Listing ---\n"
739 detail-menu
740 "\n\n"
741 "@end detailmenu\n"))
742 "@end menu\n"))
743 "\n\n"
745 ;; Document's body.
746 contents
747 "\n"
748 ;; Creator.
749 (let ((creator-info (plist-get info :with-creator)))
750 (cond
751 ((not creator-info) "")
752 ((eq creator-info 'comment)
753 (format "@c %s\n" (plist-get info :creator)))
754 (t (concat (plist-get info :creator) "\n"))))
755 ;; Document end.
756 "\n@bye")))
760 ;;; Transcode Functions
762 ;;; Bold
764 (defun org-texinfo-bold (bold contents info)
765 "Transcode BOLD from Org to Texinfo.
766 CONTENTS is the text with bold markup. INFO is a plist holding
767 contextual information."
768 (org-texinfo--text-markup contents 'bold))
770 ;;; Center Block
772 (defun org-texinfo-center-block (center-block contents info)
773 "Transcode a CENTER-BLOCK element from Org to Texinfo.
774 CONTENTS holds the contents of the block. INFO is a plist used
775 as a communication channel."
776 contents)
778 ;;; Clock
780 (defun org-texinfo-clock (clock contents info)
781 "Transcode a CLOCK element from Org to Texinfo.
782 CONTENTS is nil. INFO is a plist holding contextual
783 information."
784 (concat
785 "@noindent"
786 (format "@strong{%s} " org-clock-string)
787 (format org-texinfo-inactive-timestamp-format
788 (concat (org-translate-time
789 (org-element-property :raw-value
790 (org-element-property :value clock)))
791 (let ((time (org-element-property :duration clock)))
792 (and time (format " (%s)" time)))))
793 "@*"))
795 ;;; Code
797 (defun org-texinfo-code (code contents info)
798 "Transcode a CODE object from Org to Texinfo.
799 CONTENTS is nil. INFO is a plist used as a communication
800 channel."
801 (org-texinfo--text-markup (org-element-property :value code) 'code))
803 ;;; Comment
805 (defun org-texinfo-comment (comment contents info)
806 "Transcode a COMMENT object from Org to Texinfo.
807 CONTENTS is the text in the comment. INFO is a plist holding
808 contextual information."
809 (org-texinfo--text-markup (org-element-property :value comment) 'comment))
811 ;;; Comment Block
813 (defun org-texinfo-comment-block (comment-block contents info)
814 "Transcode a COMMENT-BLOCK object from Org to Texinfo.
815 CONTENTS is the text within the block. INFO is a plist holding
816 contextual information."
817 (format "@ignore\n%s@end ignore" (org-element-property :value comment-block)))
819 ;;; Drawer
821 (defun org-texinfo-drawer (drawer contents info)
822 "Transcode a DRAWER element from Org to Texinfo.
823 CONTENTS holds the contents of the block. INFO is a plist
824 holding contextual information."
825 (let* ((name (org-element-property :drawer-name drawer))
826 (output (if (functionp org-texinfo-format-drawer-function)
827 (funcall org-texinfo-format-drawer-function
828 name contents)
829 ;; If there's no user defined function: simply
830 ;; display contents of the drawer.
831 contents)))
832 output))
834 ;;; Dynamic Block
836 (defun org-texinfo-dynamic-block (dynamic-block contents info)
837 "Transcode a DYNAMIC-BLOCK element from Org to Texinfo.
838 CONTENTS holds the contents of the block. INFO is a plist
839 holding contextual information. See `org-export-data'."
840 contents)
842 ;;; Entity
844 (defun org-texinfo-entity (entity contents info)
845 "Transcode an ENTITY object from Org to Texinfo.
846 CONTENTS are the definition itself. INFO is a plist holding
847 contextual information."
848 (let ((ent (org-element-property :latex entity)))
849 (if (org-element-property :latex-math-p entity) (format "@math{%s}" ent) ent)))
851 ;;; Example Block
853 (defun org-texinfo-example-block (example-block contents info)
854 "Transcode an EXAMPLE-BLOCK element from Org to Texinfo.
855 CONTENTS is nil. INFO is a plist holding contextual
856 information."
857 (format "@verbatim\n%s@end verbatim"
858 (org-export-format-code-default example-block info)))
860 ;;; Export Block
862 (defun org-texinfo-export-block (export-block contents info)
863 "Transcode a EXPORT-BLOCK element from Org to Texinfo.
864 CONTENTS is nil. INFO is a plist holding contextual information."
865 (when (string= (org-element-property :type export-block) "TEXINFO")
866 (org-remove-indentation (org-element-property :value export-block))))
868 ;;; Export Snippet
870 (defun org-texinfo-export-snippet (export-snippet contents info)
871 "Transcode a EXPORT-SNIPPET object from Org to Texinfo.
872 CONTENTS is nil. INFO is a plist holding contextual information."
873 (when (eq (org-export-snippet-backend export-snippet) 'texinfo)
874 (org-element-property :value export-snippet)))
876 ;;; Fixed Width
878 (defun org-texinfo-fixed-width (fixed-width contents info)
879 "Transcode a FIXED-WIDTH element from Org to Texinfo.
880 CONTENTS is nil. INFO is a plist holding contextual information."
881 (format "@example\n%s\n@end example"
882 (org-remove-indentation
883 (org-texinfo--sanitize-content
884 (org-element-property :value fixed-width)))))
886 ;;; Footnote Reference
889 (defun org-texinfo-footnote-reference (footnote contents info)
890 "Create a footnote reference for FOOTNOTE.
892 FOOTNOTE is the footnote to define. CONTENTS is nil. INFO is a
893 plist holding contextual information."
894 (let ((def (org-export-get-footnote-definition footnote info)))
895 (format "@footnote{%s}"
896 (org-trim (org-export-data def info)))))
898 ;;; Headline
900 (defun org-texinfo-headline (headline contents info)
901 "Transcode a HEADLINE element from Org to Texinfo.
902 CONTENTS holds the contents of the headline. INFO is a plist
903 holding contextual information."
904 (let* ((class (plist-get info :texinfo-class))
905 (level (org-export-get-relative-level headline info))
906 (numberedp (org-export-numbered-headline-p headline info))
907 (class-sectionning (assoc class org-texinfo-classes))
908 ;; Find the index type, if any
909 (index (org-element-property :INDEX headline))
910 ;; Check if it is an appendix
911 (appendix (org-element-property :APPENDIX headline))
912 ;; Retrieve headline text
913 (text (org-texinfo--sanitize-headline
914 (org-element-property :title headline) info))
915 ;; Create node info, to insert it before section formatting.
916 ;; Use custom menu title if present
917 (node (format "@node %s\n" (org-texinfo--get-node headline info)))
918 ;; Menus must be generated with first child, otherwise they
919 ;; will not nest properly
920 (menu (let* ((first (org-export-first-sibling-p headline info))
921 (parent (org-export-get-parent-headline headline))
922 (title (org-texinfo--sanitize-headline
923 (org-element-property :title parent) info))
924 heading listing
925 (tree (plist-get info :parse-tree)))
926 (if first
927 (org-element-map (plist-get info :parse-tree) 'headline
928 (lambda (ref)
929 (if (member title (org-element-property :title ref))
930 (push ref heading)))
931 info t))
932 (setq listing (org-texinfo--build-menu
933 (car heading) level info))
934 (if listing
935 (setq listing (replace-regexp-in-string
936 "%" "%%" listing)
937 listing (format
938 "\n@menu\n%s\n@end menu\n\n" listing))
939 'nil)))
940 ;; Section formatting will set two placeholders: one for the
941 ;; title and the other for the contents.
942 (section-fmt
943 (let ((sec (if (and (symbolp (nth 2 class-sectionning))
944 (fboundp (nth 2 class-sectionning)))
945 (funcall (nth 2 class-sectionning) level numberedp)
946 (nth (1+ level) class-sectionning))))
947 (cond
948 ;; No section available for that LEVEL.
949 ((not sec) nil)
950 ;; Section format directly returned by a function.
951 ((stringp sec) sec)
952 ;; (numbered-section . unnumbered-section)
953 ((not (consp (cdr sec)))
954 (cond
955 ;;If an index, always unnumbered
956 (index
957 (concat menu node (cdr sec) "\n%s"))
958 (appendix
959 (concat menu node (replace-regexp-in-string
960 "unnumbered"
961 "appendix"
962 (cdr sec)) "\n%s"))
963 ;; Otherwise number as needed.
965 (concat menu node
966 (funcall
967 (if numberedp #'car #'cdr) sec) "\n%s")))))))
968 (todo
969 (and (plist-get info :with-todo-keywords)
970 (let ((todo (org-element-property :todo-keyword headline)))
971 (and todo (org-export-data todo info)))))
972 (todo-type (and todo (org-element-property :todo-type headline)))
973 (tags (and (plist-get info :with-tags)
974 (org-export-get-tags headline info)))
975 (priority (and (plist-get info :with-priority)
976 (org-element-property :priority headline)))
977 ;; Create the headline text along with a no-tag version. The
978 ;; latter is required to remove tags from table of contents.
979 (full-text (org-texinfo--sanitize-content
980 (if (functionp org-texinfo-format-headline-function)
981 ;; User-defined formatting function.
982 (funcall org-texinfo-format-headline-function
983 todo todo-type priority text tags)
984 ;; Default formatting.
985 (concat
986 (when todo
987 (format "@strong{%s} " todo))
988 (when priority (format "@emph{#%s} " priority))
989 text
990 (when tags
991 (format ":%s:"
992 (mapconcat 'identity tags ":")))))))
993 (full-text-no-tag
994 (org-texinfo--sanitize-content
995 (if (functionp org-texinfo-format-headline-function)
996 ;; User-defined formatting function.
997 (funcall org-texinfo-format-headline-function
998 todo todo-type priority text nil)
999 ;; Default formatting.
1000 (concat
1001 (when todo (format "@strong{%s} " todo))
1002 (when priority (format "@emph{#%c} " priority))
1003 text))))
1004 (pre-blanks
1005 (make-string (org-element-property :pre-blank headline) 10)))
1006 (cond
1007 ;; Case 1: This is a footnote section: ignore it.
1008 ((org-element-property :footnote-section-p headline) nil)
1009 ;; Case 2: This is the `copying' section: ignore it
1010 ;; This is used elsewhere.
1011 ((org-element-property :COPYING headline) nil)
1012 ;; Case 3: An index. If it matches one of the known indexes,
1013 ;; print it as such following the contents, otherwise
1014 ;; print the contents and leave the index up to the user.
1015 (index
1016 (format
1017 section-fmt full-text
1018 (concat pre-blanks contents "\n"
1019 (if (member index '("cp" "fn" "ky" "pg" "tp" "vr"))
1020 (concat "@printindex " index)))))
1021 ;; Case 4: This is a deep sub-tree: export it as a list item.
1022 ;; Also export as items headlines for which no section
1023 ;; format has been found.
1024 ((or (not section-fmt) (org-export-low-level-p headline info))
1025 ;; Build the real contents of the sub-tree.
1026 (let ((low-level-body
1027 (concat
1028 ;; If the headline is the first sibling, start a list.
1029 (when (org-export-first-sibling-p headline info)
1030 (format "@%s\n" (if numberedp 'enumerate 'itemize)))
1031 ;; Itemize headline
1032 "@item\n" full-text "\n" pre-blanks contents)))
1033 ;; If headline is not the last sibling simply return
1034 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1035 ;; blank line.
1036 (if (not (org-export-last-sibling-p headline info)) low-level-body
1037 (replace-regexp-in-string
1038 "[ \t\n]*\\'"
1039 (format "\n@end %s" (if numberedp 'enumerate 'itemize))
1040 low-level-body))))
1041 ;; Case 5: Standard headline. Export it as a section.
1043 (cond
1044 ((not (and tags (eq (plist-get info :with-tags) 'not-in-toc)))
1045 ;; Regular section. Use specified format string.
1046 (format (replace-regexp-in-string "%]" "%%]" section-fmt) full-text
1047 (concat pre-blanks contents)))
1048 ((string-match "\\`@\\(.*?\\){" section-fmt)
1049 ;; If tags should be removed from table of contents, insert
1050 ;; title without tags as an alternative heading in sectioning
1051 ;; command.
1052 (format (replace-match (concat (match-string 1 section-fmt) "[%s]")
1053 nil nil section-fmt 1)
1054 ;; Replace square brackets with parenthesis since
1055 ;; square brackets are not supported in optional
1056 ;; arguments.
1057 (replace-regexp-in-string
1058 "\\[" "("
1059 (replace-regexp-in-string
1060 "\\]" ")"
1061 full-text-no-tag))
1062 full-text
1063 (concat pre-blanks contents)))
1065 ;; Impossible to add an alternative heading. Fallback to
1066 ;; regular sectioning format string.
1067 (format (replace-regexp-in-string "%]" "%%]" section-fmt) full-text
1068 (concat pre-blanks contents))))))))
1070 ;;; Inline Src Block
1072 (defun org-texinfo-inline-src-block (inline-src-block contents info)
1073 "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
1074 CONTENTS holds the contents of the item. INFO is a plist holding
1075 contextual information."
1076 (let* ((code (org-element-property :value inline-src-block))
1077 (separator (org-texinfo--find-verb-separator code)))
1078 (concat "@verb{" separator code separator "}")))
1080 ;;; Inlinetask
1082 (defun org-texinfo-inlinetask (inlinetask contents info)
1083 "Transcode an INLINETASK element from Org to Texinfo.
1084 CONTENTS holds the contents of the block. INFO is a plist
1085 holding contextual information."
1086 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1087 (todo (and (plist-get info :with-todo-keywords)
1088 (let ((todo (org-element-property :todo-keyword inlinetask)))
1089 (and todo (org-export-data todo info)))))
1090 (todo-type (org-element-property :todo-type inlinetask))
1091 (tags (and (plist-get info :with-tags)
1092 (org-export-get-tags inlinetask info)))
1093 (priority (and (plist-get info :with-priority)
1094 (org-element-property :priority inlinetask))))
1095 ;; If `org-texinfo-format-inlinetask-function' is provided, call it
1096 ;; with appropriate arguments.
1097 (if (functionp org-texinfo-format-inlinetask-function)
1098 (funcall org-texinfo-format-inlinetask-function
1099 todo todo-type priority title tags contents)
1100 ;; Otherwise, use a default template.
1101 (let ((full-title
1102 (concat
1103 (when todo (format "@strong{%s} " todo))
1104 (when priority (format "#%c " priority))
1105 title
1106 (when tags (format ":%s:"
1107 (mapconcat 'identity tags ":"))))))
1108 (format (concat "@center %s\n\n"
1109 "%s"
1110 "\n")
1111 full-title contents)))))
1113 ;;; Italic
1115 (defun org-texinfo-italic (italic contents info)
1116 "Transcode ITALIC from Org to Texinfo.
1117 CONTENTS is the text with italic markup. INFO is a plist holding
1118 contextual information."
1119 (org-texinfo--text-markup contents 'italic))
1121 ;;; Item
1123 (defun org-texinfo-item (item contents info)
1124 "Transcode an ITEM element from Org to Texinfo.
1125 CONTENTS holds the contents of the item. INFO is a plist holding
1126 contextual information."
1127 (let* ((tag (org-element-property :tag item))
1128 (desc (org-export-data tag info)))
1129 (concat "\n@item " (if tag desc) "\n"
1130 (org-trim contents) "\n")))
1132 ;;; Keyword
1134 (defun org-texinfo-keyword (keyword contents info)
1135 "Transcode a KEYWORD element from Org to Texinfo.
1136 CONTENTS is nil. INFO is a plist holding contextual information."
1137 (let ((key (org-element-property :key keyword))
1138 (value (org-element-property :value keyword)))
1139 (cond
1140 ((string= key "TEXINFO") value)
1141 ((string= key "CINDEX") (format "@cindex %s" value))
1142 ((string= key "FINDEX") (format "@findex %s" value))
1143 ((string= key "KINDEX") (format "@kindex %s" value))
1144 ((string= key "PINDEX") (format "@pindex %s" value))
1145 ((string= key "TINDEX") (format "@tindex %s" value))
1146 ((string= key "VINDEX") (format "@vindex %s" value)))))
1148 ;;; Line Break
1150 (defun org-texinfo-line-break (line-break contents info)
1151 "Transcode a LINE-BREAK object from Org to Texinfo.
1152 CONTENTS is nil. INFO is a plist holding contextual information."
1153 "@*\n")
1155 ;;; Link
1157 (defun org-texinfo-link (link desc info)
1158 "Transcode a LINK object from Org to Texinfo.
1160 DESC is the description part of the link, or the empty string.
1161 INFO is a plist holding contextual information. See
1162 `org-export-data'."
1163 (let* ((type (org-element-property :type link))
1164 (raw-path (org-element-property :path link))
1165 ;; Ensure DESC really exists, or set it to nil.
1166 (desc (and (not (string= desc "")) desc))
1167 (path (cond
1168 ((member type '("http" "https" "ftp"))
1169 (concat type ":" raw-path))
1170 ((string= type "file")
1171 (if (file-name-absolute-p raw-path)
1172 (concat "file://" (expand-file-name raw-path))
1173 (concat "file://" raw-path)))
1174 (t raw-path)))
1175 (email (if (string= type "mailto")
1176 (let ((text (replace-regexp-in-string
1177 "@" "@@" raw-path)))
1178 (concat text (if desc (concat "," desc))))))
1179 protocol)
1180 (cond
1181 ;; Links pointing to a headline: Find destination and build
1182 ;; appropriate referencing command.
1183 ((member type '("custom-id" "id"))
1184 (let ((destination (org-export-resolve-id-link link info)))
1185 (case (org-element-type destination)
1186 ;; Id link points to an external file.
1187 (plain-text
1188 (if desc (format "@uref{file://%s,%s}" destination desc)
1189 (format "@uref{file://%s}" destination)))
1190 ;; LINK points to a headline. Use the headline as the NODE target
1191 (headline
1192 (format "@ref{%s,%s}"
1193 (org-texinfo--get-node destination info)
1194 (or desc "")))
1195 (otherwise
1196 (let ((path (org-export-solidify-link-text path)))
1197 (if (not desc) (format "@ref{%s}" path)
1198 (format "@ref{%s,,%s}" path desc)))))))
1199 ((member type '("info"))
1200 (let* ((info-path (split-string path "[:#]"))
1201 (info-manual (car info-path))
1202 (info-node (or (cadr info-path) "top"))
1203 (title (or desc "")))
1204 (format "@ref{%s,%s,,%s,}" info-node title info-manual)))
1205 ((member type '("fuzzy"))
1206 (let ((destination (org-export-resolve-fuzzy-link link info)))
1207 (case (org-element-type destination)
1208 ;; Id link points to an external file.
1209 (plain-text
1210 (if desc (format "@uref{file://%s,%s}" destination desc)
1211 (format "@uref{file://%s}" destination)))
1212 ;; LINK points to a headline. Use the headline as the NODE target
1213 (headline
1214 (format "@ref{%s,%s}"
1215 (org-texinfo--get-node destination info)
1216 (or desc "")))
1217 (otherwise
1218 (let ((path (org-export-solidify-link-text path)))
1219 (if (not desc) (format "@ref{%s}" path)
1220 (format "@ref{%s,,%s}" path desc)))))))
1221 ;; Special case for email addresses
1222 (email
1223 (format "@email{%s}" email))
1224 ;; External link with a description part.
1225 ((and path desc) (format "@uref{%s,%s}" path desc))
1226 ;; External link without a description part.
1227 (path (format "@uref{%s}" path))
1228 ;; No path, only description. Try to do something useful.
1229 (t (format org-texinfo-link-with-unknown-path-format desc)))))
1232 ;;; Menu
1234 (defun org-texinfo-make-menu (info level)
1235 "Create the menu for inclusion in the texifo document.
1237 INFO is the parsed buffer that contains the headlines. LEVEL
1238 determines whether to make the main menu, or the detailed menu.
1240 This is only used for generating the primary menu. In-Node menus
1241 are generated directly."
1242 (let ((parse (plist-get info :parse-tree)))
1243 (cond
1244 ;; Generate the main menu
1245 ((eq level 'main) (org-texinfo--build-menu parse 1 info))
1246 ;; Generate the detailed (recursive) menu
1247 ((eq level 'detailed)
1248 ;; Requires recursion
1249 ;;(org-texinfo--build-detailed-menu parse top info)
1250 (org-texinfo--build-menu parse 1 info 'detailed)))))
1252 ;;; Paragraph
1254 (defun org-texinfo-paragraph (paragraph contents info)
1255 "Transcode a PARAGRAPH element from Org to Texinfo.
1256 CONTENTS is the contents of the paragraph, as a string. INFO is
1257 the plist used as a communication channel."
1258 contents)
1260 ;;; Plain List
1262 (defun org-texinfo-plain-list (plain-list contents info)
1263 "Transcode a PLAIN-LIST element from Org to Texinfo.
1264 CONTENTS is the contents of the list. INFO is a plist holding
1265 contextual information."
1266 (let* ((attr (org-export-read-attribute :attr_texinfo plain-list))
1267 (indic (or (plist-get attr :indic)
1268 org-texinfo-def-table-markup))
1269 (type (org-element-property :type plain-list))
1270 (table-type (or (plist-get attr :table-type)
1271 "table"))
1272 ;; Ensure valid texinfo table type.
1273 (table-type (if (memq table-type '("table" "ftable" "vtable"))
1274 table-type
1275 "table"))
1276 (list-type (cond
1277 ((eq type 'ordered) "enumerate")
1278 ((eq type 'unordered) "itemize")
1279 ((eq type 'descriptive) table-type))))
1280 (format "@%s%s\n@end %s"
1281 (if (eq type 'descriptive)
1282 (concat list-type " " indic)
1283 list-type)
1284 contents
1285 list-type)))
1287 ;;; Plain Text
1289 (defun org-texinfo-plain-text (text info)
1290 "Transcode a TEXT string from Org to Texinfo.
1291 TEXT is the string to transcode. INFO is a plist holding
1292 contextual information."
1293 ;; First protect @, { and }.
1294 (let ((output (org-texinfo--sanitize-content text)))
1295 ;; Activate smart quotes. Be sure to provide original TEXT string
1296 ;; since OUTPUT may have been modified.
1297 (when (plist-get info :with-smart-quotes)
1298 (setq output
1299 (org-export-activate-smart-quotes output :texinfo info text)))
1300 ;; LaTeX into @LaTeX{} and TeX into @TeX{}
1301 (let ((case-fold-search nil)
1302 (start 0))
1303 (while (string-match "\\(\\(?:La\\)?TeX\\)" output start)
1304 (setq output (replace-match
1305 (format "@%s{}" (match-string 1 output)) nil t output)
1306 start (match-end 0))))
1307 ;; Convert special strings.
1308 (when (plist-get info :with-special-strings)
1309 (while (string-match (regexp-quote "...") output)
1310 (setq output (replace-match "@dots{}" nil t output))))
1311 ;; Handle break preservation if required.
1312 (when (plist-get info :preserve-breaks)
1313 (setq output (replace-regexp-in-string
1314 "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" output)))
1315 ;; Return value.
1316 output))
1318 ;;; Planning
1320 (defun org-texinfo-planning (planning contents info)
1321 "Transcode a PLANNING element from Org to Texinfo.
1322 CONTENTS is nil. INFO is a plist holding contextual
1323 information."
1324 (concat
1325 "@noindent"
1326 (mapconcat
1327 'identity
1328 (delq nil
1329 (list
1330 (let ((closed (org-element-property :closed planning)))
1331 (when closed
1332 (concat
1333 (format "@strong{%s} " org-closed-string)
1334 (format org-texinfo-inactive-timestamp-format
1335 (org-translate-time
1336 (org-element-property :raw-value closed))))))
1337 (let ((deadline (org-element-property :deadline planning)))
1338 (when deadline
1339 (concat
1340 (format "@strong{%s} " org-deadline-string)
1341 (format org-texinfo-active-timestamp-format
1342 (org-translate-time
1343 (org-element-property :raw-value deadline))))))
1344 (let ((scheduled (org-element-property :scheduled planning)))
1345 (when scheduled
1346 (concat
1347 (format "@strong{%s} " org-scheduled-string)
1348 (format org-texinfo-active-timestamp-format
1349 (org-translate-time
1350 (org-element-property :raw-value scheduled))))))))
1351 " ")
1352 "@*"))
1354 ;;; Property Drawer
1356 (defun org-texinfo-property-drawer (property-drawer contents info)
1357 "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
1358 CONTENTS is nil. INFO is a plist holding contextual
1359 information."
1360 ;; The property drawer isn't exported but we want separating blank
1361 ;; lines nonetheless.
1364 ;;; Quote Block
1366 (defun org-texinfo-quote-block (quote-block contents info)
1367 "Transcode a QUOTE-BLOCK element from Org to Texinfo.
1368 CONTENTS holds the contents of the block. INFO is a plist
1369 holding contextual information."
1370 (let* ((title (org-element-property :name quote-block))
1371 (start-quote (concat "@quotation"
1372 (if title
1373 (format " %s" title)))))
1374 (format "%s\n%s@end quotation" start-quote contents)))
1376 ;;; Quote Section
1378 (defun org-texinfo-quote-section (quote-section contents info)
1379 "Transcode a QUOTE-SECTION element from Org to Texinfo.
1380 CONTENTS is nil. INFO is a plist holding contextual information."
1381 (let ((value (org-remove-indentation
1382 (org-element-property :value quote-section))))
1383 (when value (format "@verbatim\n%s@end verbatim" value))))
1385 ;;; Radio Target
1387 (defun org-texinfo-radio-target (radio-target text info)
1388 "Transcode a RADIO-TARGET object from Org to Texinfo.
1389 TEXT is the text of the target. INFO is a plist holding
1390 contextual information."
1391 (format "@anchor{%s}%s"
1392 (org-export-solidify-link-text
1393 (org-element-property :value radio-target))
1394 text))
1396 ;;; Section
1398 (defun org-texinfo-section (section contents info)
1399 "Transcode a SECTION element from Org to Texinfo.
1400 CONTENTS holds the contents of the section. INFO is a plist
1401 holding contextual information."
1402 contents)
1404 ;;; Special Block
1406 (defun org-texinfo-special-block (special-block contents info)
1407 "Transcode a SPECIAL-BLOCK element from Org to Texinfo.
1408 CONTENTS holds the contents of the block. INFO is a plist used
1409 as a communication channel."
1410 contents)
1412 ;;; Src Block
1414 (defun org-texinfo-src-block (src-block contents info)
1415 "Transcode a SRC-BLOCK element from Org to Texinfo.
1416 CONTENTS holds the contents of the item. INFO is a plist holding
1417 contextual information."
1418 (let* ((lang (org-element-property :language src-block))
1419 (lisp-p (string-match-p "lisp" lang)))
1420 (cond
1421 ;; Case 1. Lisp Block
1422 (lisp-p
1423 (format "@lisp\n%s@end lisp"
1424 (org-export-format-code-default src-block info)))
1425 ;; Case 2. Other blocks
1427 (format "@example\n%s@end example"
1428 (org-export-format-code-default src-block info))))))
1430 ;;; Statistics Cookie
1432 (defun org-texinfo-statistics-cookie (statistics-cookie contents info)
1433 "Transcode a STATISTICS-COOKIE object from Org to Texinfo.
1434 CONTENTS is nil. INFO is a plist holding contextual information."
1435 (org-element-property :value statistics-cookie))
1437 ;;; Subscript
1439 (defun org-texinfo-subscript (subscript contents info)
1440 "Transcode a SUBSCRIPT 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 ;;; Superscript
1447 (defun org-texinfo-superscript (superscript contents info)
1448 "Transcode a SUPERSCRIPT object from Org to Texinfo.
1449 CONTENTS is the contents of the object. INFO is a plist holding
1450 contextual information."
1451 (format "@math{^%s}" contents))
1453 ;;; Table
1455 ;; `org-texinfo-table' is the entry point for table transcoding. It
1456 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
1457 ;; delegates the job to either `org-texinfo-table--table.el-table' or
1458 ;; `org-texinfo-table--org-table' functions, depending of the type of
1459 ;; the table.
1461 ;; `org-texinfo-table--align-string' is a subroutine used to build
1462 ;; alignment string for Org tables.
1464 (defun org-texinfo-table (table contents info)
1465 "Transcode a TABLE element from Org to Texinfo.
1466 CONTENTS is the contents of the table. INFO is a plist holding
1467 contextual information."
1468 (cond
1469 ;; Case 1: verbatim table.
1470 ((or org-texinfo-tables-verbatim
1471 (let ((attr (mapconcat 'identity
1472 (org-element-property :attr_latex table)
1473 " ")))
1474 (and attr (string-match "\\<verbatim\\>" attr))))
1475 (format "@verbatim \n%s\n@end verbatim"
1476 ;; Re-create table, without affiliated keywords.
1477 (org-trim
1478 (org-element-interpret-data
1479 `(table nil ,@(org-element-contents table))))))
1480 ;; Case 2: table.el table. Convert it using appropriate tools.
1481 ((eq (org-element-property :type table) 'table.el)
1482 (org-texinfo-table--table.el-table table contents info))
1483 ;; Case 3: Standard table.
1484 (t (org-texinfo-table--org-table table contents info))))
1486 (defun org-texinfo-table-column-widths (table info)
1487 "Determine the largest table cell in each column to process alignment.
1489 TABLE is the table element to transcode. INFO is a plist used as
1490 a communication channel."
1491 (let* ((rows (org-element-map table 'table-row 'identity info))
1492 (collected (loop for row in rows collect
1493 (org-element-map row 'table-cell 'identity info)))
1494 (number-cells (length (car collected)))
1495 cells counts)
1496 (loop for row in collected do
1497 (push (mapcar (lambda (ref)
1498 (let* ((start (org-element-property :contents-begin ref))
1499 (end (org-element-property :contents-end ref))
1500 (length (- end start)))
1501 length)) row) cells))
1502 (setq cells (org-remove-if 'null cells))
1503 (push (loop for count from 0 to (- number-cells 1) collect
1504 (loop for item in cells collect
1505 (nth count item))) counts)
1506 (mapconcat (lambda (size)
1507 (make-string size ?a)) (mapcar (lambda (ref)
1508 (apply 'max `,@ref)) (car counts))
1509 "} {")))
1511 (defun org-texinfo-table--org-table (table contents info)
1512 "Return appropriate Texinfo code for an Org table.
1514 TABLE is the table type element to transcode. CONTENTS is its
1515 contents, as a string. INFO is a plist used as a communication
1516 channel.
1518 This function assumes TABLE has `org' as its `:type' attribute."
1519 (let* ((attr (org-export-read-attribute :attr_texinfo table))
1520 (col-width (plist-get attr :columns))
1521 (columns (if col-width
1522 (format "@columnfractions %s"
1523 col-width)
1524 (format "{%s}"
1525 (org-texinfo-table-column-widths
1526 table info)))))
1527 ;; Prepare the final format string for the table.
1528 (cond
1529 ;; Longtable.
1530 ;; Others.
1531 (t (concat
1532 (format "@multitable %s\n%s@end multitable"
1533 columns
1534 contents))))))
1536 (defun org-texinfo-table--table.el-table (table contents info)
1537 "Returns nothing.
1539 Rather than return an invalid table, nothing is returned."
1540 'nil)
1542 ;;; Table Cell
1544 (defun org-texinfo-table-cell (table-cell contents info)
1545 "Transcode a TABLE-CELL element from Org to Texinfo.
1546 CONTENTS is the cell contents. INFO is a plist used as
1547 a communication channel."
1548 (concat (if (and contents
1549 org-texinfo-table-scientific-notation
1550 (string-match orgtbl-exp-regexp contents))
1551 ;; Use appropriate format string for scientific
1552 ;; notation.
1553 (format org-texinfo-table-scientific-notation
1554 (match-string 1 contents)
1555 (match-string 2 contents))
1556 contents)
1557 (when (org-export-get-next-element table-cell info) "\n@tab ")))
1559 ;;; Table Row
1561 (defun org-texinfo-table-row (table-row contents info)
1562 "Transcode a TABLE-ROW element from Org to Texinfo.
1563 CONTENTS is the contents of the row. INFO is a plist used as
1564 a communication channel."
1565 ;; Rules are ignored since table separators are deduced from
1566 ;; borders of the current row.
1567 (when (eq (org-element-property :type table-row) 'standard)
1568 (let ((rowgroup-tag
1569 (cond
1570 ;; Case 1: Belongs to second or subsequent rowgroup.
1571 ((not (= 1 (org-export-table-row-group table-row info)))
1572 "@item ")
1573 ;; Case 2: Row is from first rowgroup. Table has >=1 rowgroups.
1574 ((org-export-table-has-header-p
1575 (org-export-get-parent-table table-row) info)
1576 "@headitem ")
1577 ;; Case 3: Row is from first and only row group.
1578 (t "@item "))))
1579 (when (eq (org-element-property :type table-row) 'standard)
1580 (concat rowgroup-tag contents "\n")))))
1582 ;;; Target
1584 (defun org-texinfo-target (target contents info)
1585 "Transcode a TARGET object from Org to Texinfo.
1586 CONTENTS is nil. INFO is a plist holding contextual
1587 information."
1588 (format "@anchor{%s}"
1589 (org-export-solidify-link-text (org-element-property :value target))))
1591 ;;; Timestamp
1593 (defun org-texinfo-timestamp (timestamp contents info)
1594 "Transcode a TIMESTAMP object from Org to Texinfo.
1595 CONTENTS is nil. INFO is a plist holding contextual
1596 information."
1597 (let ((value (org-texinfo-plain-text
1598 (org-timestamp-translate timestamp) info)))
1599 (case (org-element-property :type timestamp)
1600 ((active active-range)
1601 (format org-texinfo-active-timestamp-format value))
1602 ((inactive inactive-range)
1603 (format org-texinfo-inactive-timestamp-format value))
1604 (t (format org-texinfo-diary-timestamp-format value)))))
1606 ;;; Verbatim
1608 (defun org-texinfo-verbatim (verbatim contents info)
1609 "Transcode a VERBATIM object from Org to Texinfo.
1610 CONTENTS is nil. INFO is a plist used as a communication
1611 channel."
1612 (org-texinfo--text-markup (org-element-property :value verbatim) 'verbatim))
1614 ;;; Verse Block
1616 (defun org-texinfo-verse-block (verse-block contents info)
1617 "Transcode a VERSE-BLOCK element from Org to Texinfo.
1618 CONTENTS is verse block contents. INFO is a plist holding
1619 contextual information."
1620 ;; In a verse environment, add a line break to each newline
1621 ;; character and change each white space at beginning of a line
1622 ;; into a space of 1 em. Also change each blank line with
1623 ;; a vertical space of 1 em.
1624 (progn
1625 (setq contents (replace-regexp-in-string
1626 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
1627 (replace-regexp-in-string
1628 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
1629 (while (string-match "^[ \t]+" contents)
1630 (let ((new-str (format "\\hspace*{%dem}"
1631 (length (match-string 0 contents)))))
1632 (setq contents (replace-match new-str nil t contents))))
1633 (format "\\begin{verse}\n%s\\end{verse}" contents)))
1636 ;;; Interactive functions
1638 (defun org-texinfo-export-to-texinfo
1639 (&optional async subtreep visible-only body-only ext-plist)
1640 "Export current buffer to a Texinfo file.
1642 If narrowing is active in the current buffer, only export its
1643 narrowed part.
1645 If a region is active, export that region.
1647 A non-nil optional argument ASYNC means the process should happen
1648 asynchronously. The resulting file should be accessible through
1649 the `org-export-stack' interface.
1651 When optional argument SUBTREEP is non-nil, export the sub-tree
1652 at point, extracting information from the headline properties
1653 first.
1655 When optional argument VISIBLE-ONLY is non-nil, don't export
1656 contents of hidden elements.
1658 When optional argument BODY-ONLY is non-nil, only write code
1659 between \"\\begin{document}\" and \"\\end{document}\".
1661 EXT-PLIST, when provided, is a property list with external
1662 parameters overriding Org default settings, but still inferior to
1663 file-local settings.
1665 Return output file's name."
1666 (interactive)
1667 (let ((outfile (org-export-output-file-name ".texi" subtreep)))
1668 (if async
1669 (org-export-async-start
1670 (lambda (f) (org-export-add-to-stack f 'texinfo))
1671 `(expand-file-name
1672 (org-export-to-file
1673 'texinfo ,outfile ,subtreep ,visible-only ,body-only
1674 ',ext-plist)))
1675 (org-export-to-file
1676 'texinfo outfile subtreep visible-only body-only ext-plist))))
1678 (defun org-texinfo-export-to-info
1679 (&optional async subtreep visible-only body-only ext-plist)
1680 "Export current buffer to Texinfo then process through to INFO.
1682 If narrowing is active in the current buffer, only export its
1683 narrowed part.
1685 If a region is active, export that region.
1687 A non-nil optional argument ASYNC means the process should happen
1688 asynchronously. The resulting file should be accessible through
1689 the `org-export-stack' interface.
1691 When optional argument SUBTREEP is non-nil, export the sub-tree
1692 at point, extracting information from the headline properties
1693 first.
1695 When optional argument VISIBLE-ONLY is non-nil, don't export
1696 contents of hidden elements.
1698 When optional argument BODY-ONLY is non-nil, only write code
1699 between \"\\begin{document}\" and \"\\end{document}\".
1701 EXT-PLIST, when provided, is a property list with external
1702 parameters overriding Org default settings, but still inferior to
1703 file-local settings.
1705 When optional argument PUB-DIR is set, use it as the publishing
1706 directory.
1708 Return INFO file's name."
1709 (interactive)
1710 (if async
1711 (let ((outfile (org-export-output-file-name ".texi" subtreep)))
1712 (org-export-async-start
1713 (lambda (f) (org-export-add-to-stack f 'texinfo))
1714 `(expand-file-name
1715 (org-texinfo-compile
1716 (org-export-to-file
1717 'texinfo ,outfile ,subtreep ,visible-only ,body-only
1718 ',ext-plist)))))
1719 (org-texinfo-compile
1720 (org-texinfo-export-to-texinfo
1721 nil subtreep visible-only body-only ext-plist))))
1723 (defun org-texinfo-compile (file)
1724 "Compile a texinfo file.
1726 FILE is the name of the file being compiled. Processing is
1727 done through the command specified in `org-texinfo-info-process'.
1729 Return INFO file name or an error if it couldn't be produced."
1730 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1731 (full-name (file-truename file))
1732 (out-dir (file-name-directory file))
1733 ;; Make sure `default-directory' is set to FILE directory,
1734 ;; not to whatever value the current buffer may have.
1735 (default-directory (file-name-directory full-name))
1736 errors)
1737 (message (format "Processing Texinfo file %s ..." file))
1738 (save-window-excursion
1739 (cond
1740 ;; A function is provided: Apply it.
1741 ((functionp org-texinfo-info-process)
1742 (funcall org-texinfo-info-process (shell-quote-argument file)))
1743 ;; A list is provided: Replace %b, %f and %o with appropriate
1744 ;; values in each command before applying it. Output is
1745 ;; redirected to "*Org INFO Texinfo Output*" buffer.
1746 ((consp org-texinfo-info-process)
1747 (let ((outbuf (get-buffer-create "*Org INFO Texinfo Output*")))
1748 (mapc
1749 (lambda (command)
1750 (shell-command
1751 (replace-regexp-in-string
1752 "%b" (shell-quote-argument base-name)
1753 (replace-regexp-in-string
1754 "%f" (shell-quote-argument full-name)
1755 (replace-regexp-in-string
1756 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1757 outbuf))
1758 org-texinfo-info-process)
1759 ;; Collect standard errors from output buffer.
1760 (setq errors (org-texinfo-collect-errors outbuf))))
1761 (t (error "No valid command to process to Info")))
1762 (let ((infofile (concat out-dir base-name ".info")))
1763 ;; Check for process failure. Provide collected errors if
1764 ;; possible.
1765 (if (not (file-exists-p infofile))
1766 (error (concat (format "INFO file %s wasn't produced" infofile)
1767 (when errors (concat ": " errors))))
1768 ;; Else remove log files, when specified, and signal end of
1769 ;; process to user, along with any error encountered.
1770 (message (concat "Process completed"
1771 (if (not errors) "."
1772 (concat " with errors: " errors)))))
1773 ;; Return output file name.
1774 infofile))))
1776 (defun org-texinfo-collect-errors (buffer)
1777 "Collect some kind of errors from \"makeinfo\" command output.
1779 BUFFER is the buffer containing output.
1781 Return collected error types as a string, or nil if there was
1782 none."
1783 (with-current-buffer buffer
1784 (save-excursion
1785 (goto-char (point-min))
1786 ;; Find final "makeinfo" run.
1787 (when t
1788 (let ((case-fold-search t)
1789 (errors ""))
1790 (when (save-excursion
1791 (re-search-forward "perhaps incorrect sectioning?" nil t))
1792 (setq errors (concat errors " [incorrect sectioning]")))
1793 (when (save-excursion
1794 (re-search-forward "missing close brace" nil t))
1795 (setq errors (concat errors " [syntax error]")))
1796 (when (save-excursion
1797 (re-search-forward "Unknown command" nil t))
1798 (setq errors (concat errors " [undefined @command]")))
1799 (when (save-excursion
1800 (re-search-forward "No matching @end" nil t))
1801 (setq errors (concat errors " [block incomplete]")))
1802 (when (save-excursion
1803 (re-search-forward "requires a sectioning" nil t))
1804 (setq errors (concat errors " [invalid section command]")))
1805 (when (save-excursion
1806 (re-search-forward "\\[unexpected\]" nil t))
1807 (setq errors (concat errors " [unexpected error]")))
1808 (when (save-excursion
1809 (re-search-forward "misplaced " nil t))
1810 (setq errors (concat errors " [syntax error]")))
1811 (and (org-string-nw-p errors) (org-trim errors)))))))
1814 (provide 'ox-texinfo)
1816 ;; Local variables:
1817 ;; generated-autoload-file: "org-loaddefs.el"
1818 ;; End:
1820 ;;; ox-texinfo.el ends here