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