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