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