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