org-export: Fix inclusion of files with no final newline
[org-mode.git] / contrib / lisp / org-e-texinfo.el
blob79fa2ed0914eb9cf8a21537d83539dfc1b609903
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))))
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 ;; Return the list of headlines (reverse to have in actual order)
555 (reverse seq)))
557 (defun org-e-texinfo--generate-menu-items (items info)
558 "Generate a list of headline information from the listing ITEMS.
560 ITEMS is a list of the headlines to be converted into entries.
561 INFO is a plist containing contextual information.
563 Returns a list containing the following information from each
564 headline: length, title, description. This is used to format the
565 menu using `org-e-texinfo--format-menu'."
566 (loop for headline in items collect
567 (let* ((title (org-e-texinfo--sanitize-menu
568 (org-e-texinfo--sanitize-headline
569 (org-element-property :title headline) info)))
570 (descr (org-export-data
571 (org-element-property :description headline) info))
572 (len (length title))
573 (output (list len title descr)))
574 output)))
576 (defun org-e-texinfo--menu-headlines (headline info)
577 "Retrieve the title from HEADLINE.
579 INFO is a plist holding contextual information.
581 Return the headline as a list of (length title description) with
582 length of -1 and nil description. This is used in
583 `org-e-texinfo--format-menu' to identify headlines as opposed to
584 entries."
585 (let ((title (org-export-data
586 (org-element-property :title headline) info)))
587 (list -1 title 'nil)))
589 (defun org-e-texinfo--format-menu (text-menu)
590 "Format the TEXT-MENU items to be properly printed in the menu.
592 Each entry in the menu should be provided as (length title
593 description).
595 Headlines in the detailed menu are given length -1 to ensure they
596 are never confused with other entries. They also have no
597 description.
599 Other menu items are output as:
600 Title:: description
602 With the spacing between :: and description based on the length
603 of the longest menu entry."
605 (let* ((lengths (mapcar 'car text-menu))
606 (max-length (apply 'max lengths))
607 output)
608 (setq output
609 (mapcar (lambda (name)
610 (let* ((title (nth 1 name))
611 (desc (nth 2 name))
612 (length (nth 0 name)))
613 (if (> length -1)
614 (concat "* " title ":: "
615 (make-string
616 (- (+ 3 max-length) length)
617 ?\s)
618 (if desc
619 (concat desc)))
620 (concat "\n" title "\n"))))
621 text-menu))
622 output))
624 ;;; Template
626 (defun org-e-texinfo-template (contents info)
627 "Return complete document string after Texinfo conversion.
628 CONTENTS is the transcoded contents string. INFO is a plist
629 holding export options."
630 (let* ((title (org-export-data (plist-get info :title) info))
631 (info-filename (or (plist-get info :texinfo-filename)
632 (file-name-nondirectory
633 (org-export-output-file-name ".info"))))
634 (author (org-export-data (plist-get info :author) info))
635 (texinfo-header (plist-get info :texinfo-header))
636 (subtitle (plist-get info :subtitle))
637 (subauthor (plist-get info :subauthor))
638 (class (plist-get info :texinfo-class))
639 (header (nth 1 (assoc class org-e-texinfo-classes)))
640 (copying (org-e-texinfo--find-copying info))
641 (dircat (plist-get info :texinfo-dircat))
642 (dirtitle (plist-get info :texinfo-dirtitle))
643 (dirdesc (plist-get info :texinfo-dirdesc))
644 ;; Spacing to align description (column 32 - 3 for `* ' and
645 ;; `.' in text.
646 (dirspacing (- 29 (length dirtitle)))
647 (menu (org-e-texinfo-make-menu info 'main))
648 (detail-menu (org-e-texinfo-make-menu info 'detailed)))
649 (concat
650 ;; Header
651 header "\n"
652 "@c %**start of header\n"
653 ;; Filename and Title
654 "@setfilename " info-filename "\n"
655 "@settitle " title "\n"
656 "\n\n"
657 "@c Version and Contact Info\n"
658 "@set AUTHOR " author "\n"
660 ;; Additional Header Options set by `#+TEXINFO_HEADER
661 (if texinfo-header
662 (concat "\n"
663 texinfo-header
664 "\n"))
666 "@c %**end of header\n"
667 "@finalout\n"
668 "\n\n"
670 ;; Copying
671 "@copying\n"
672 ;; Only export the content of the headline, do not need the
673 ;; initial headline.
674 (org-export-data (nth 2 copying) info)
675 "@end copying\n"
676 "\n\n"
678 ;; Info directory information
679 ;; Only supply if both title and category are provided
680 (if (and dircat dirtitle)
681 (concat "@dircategory " dircat "\n"
682 "@direntry\n"
683 "* " dirtitle "."
684 (make-string dirspacing ?\s)
685 dirdesc "\n"
686 "@end direntry\n"))
687 "\n\n"
689 ;; Title
690 "@titlepage\n"
691 "@title " title "\n\n"
692 (if subtitle
693 (concat "@subtitle " subtitle "\n"))
694 "@author " author "\n"
695 (if subauthor
696 (concat subauthor "\n"))
697 "\n"
698 "@c The following two commands start the copyright page.\n"
699 "@page\n"
700 "@vskip 0pt plus 1filll\n"
701 "@insertcopying\n"
702 "@end titlepage\n\n"
703 "@c Output the table of contents at the beginning.\n"
704 "@contents\n\n"
706 ;; Configure Top Node when not for Tex
707 "@ifnottex\n"
708 "@node Top\n"
709 "@top " title " Manual\n"
710 "@insertcopying\n"
711 "@end ifnottex\n\n"
713 ;; Do not output menus if they are empty
714 (if menu
715 ;; Menu
716 (concat "@menu\n"
717 menu
718 "\n\n"
719 ;; Detailed Menu
720 (if detail-menu
721 (concat "@detailmenu\n"
722 " --- The Detailed Node Listing ---\n"
723 detail-menu
724 "\n\n"
725 "@end detailmenu\n"))
726 "@end menu\n"))
727 "\n\n"
729 ;; Document's body.
730 contents
731 "\n"
732 ;; Creator.
733 (let ((creator-info (plist-get info :with-creator)))
734 (cond
735 ((not creator-info) "")
736 ((eq creator-info 'comment)
737 (format "@c %s\n" (plist-get info :creator)))
738 (t (concat (plist-get info :creator) "\n"))))
739 ;; Document end.
740 "\n@bye")))
744 ;;; Transcode Functions
746 ;;; Bold
748 (defun org-e-texinfo-bold (bold contents info)
749 "Transcode BOLD from Org to Texinfo.
750 CONTENTS is the text with bold markup. INFO is a plist holding
751 contextual information."
752 (org-e-texinfo--text-markup contents 'bold))
754 ;;; Center Block
756 (defun org-e-texinfo-center-block (center-block contents info)
757 "Transcode a CENTER-BLOCK element from Org to Texinfo.
758 CONTENTS holds the contents of the block. INFO is a plist used
759 as a communication channel."
760 contents)
762 ;;; Clock
764 (defun org-e-texinfo-clock (clock contents info)
765 "Transcode a CLOCK element from Org to Texinfo.
766 CONTENTS is nil. INFO is a plist holding contextual
767 information."
768 (concat
769 "@noindent"
770 (format "@strong{%s} " org-clock-string)
771 (format org-e-texinfo-inactive-timestamp-format
772 (concat (org-translate-time (org-element-property :value clock))
773 (let ((time (org-element-property :time clock)))
774 (and time (format " (%s)" time)))))
775 "@*"))
777 ;;; Code
779 (defun org-e-texinfo-code (code contents info)
780 "Transcode a CODE object from Org to Texinfo.
781 CONTENTS is nil. INFO is a plist used as a communication
782 channel."
783 (org-e-texinfo--text-markup (org-element-property :value code) 'code))
785 ;;; Comment
787 (defun org-e-texinfo-comment (comment contents info)
788 "Transcode a COMMENT object from Org to Texinfo.
789 CONTENTS is the text in the comment. INFO is a plist holding
790 contextual information."
791 (org-e-texinfo--text-markup (org-element-property :value comment) 'comment))
793 ;;; Comment Block
795 (defun org-e-texinfo-comment-block (comment-block contents info)
796 "Transcode a COMMENT-BLOCK object from Org to Texinfo.
797 CONTENTS is the text within the block. INFO is a plist holding
798 contextual information."
799 (format "@ignore\n%s@end ignore" (org-element-property :value comment-block)))
801 ;;; Drawer
803 (defun org-e-texinfo-drawer (drawer contents info)
804 "Transcode a DRAWER element from Org to Texinfo.
805 CONTENTS holds the contents of the block. INFO is a plist
806 holding contextual information."
807 (let* ((name (org-element-property :drawer-name drawer))
808 (output (if (functionp org-e-texinfo-format-drawer-function)
809 (funcall org-e-texinfo-format-drawer-function
810 name contents)
811 ;; If there's no user defined function: simply
812 ;; display contents of the drawer.
813 contents)))
814 output))
816 ;;; Dynamic Block
818 (defun org-e-texinfo-dynamic-block (dynamic-block contents info)
819 "Transcode a DYNAMIC-BLOCK element from Org to Texinfo.
820 CONTENTS holds the contents of the block. INFO is a plist
821 holding contextual information. See `org-export-data'."
822 contents)
824 ;;; Entity
826 (defun org-e-texinfo-entity (entity contents info)
827 "Transcode an ENTITY object from Org to Texinfo.
828 CONTENTS are the definition itself. INFO is a plist holding
829 contextual information."
830 (let ((ent (org-element-property :latex entity)))
831 (if (org-element-property :latex-math-p entity) (format "@math{%s}" ent) ent)))
833 ;;; Example Block
835 (defun org-e-texinfo-example-block (example-block contents info)
836 "Transcode an EXAMPLE-BLOCK element from Org to Texinfo.
837 CONTENTS is nil. INFO is a plist holding contextual
838 information."
839 (format "@verbatim\n%s@end verbatim"
840 (org-export-format-code-default example-block info)))
842 ;;; Export Block
844 (defun org-e-texinfo-export-block (export-block contents info)
845 "Transcode a EXPORT-BLOCK element from Org to Texinfo.
846 CONTENTS is nil. INFO is a plist holding contextual information."
847 (when (string= (org-element-property :type export-block) "TEXINFO")
848 (org-remove-indentation (org-element-property :value export-block))))
850 ;;; Export Snippet
852 (defun org-e-texinfo-export-snippet (export-snippet contents info)
853 "Transcode a EXPORT-SNIPPET object from Org to Texinfo.
854 CONTENTS is nil. INFO is a plist holding contextual information."
855 (when (eq (org-export-snippet-backend export-snippet) 'e-texinfo)
856 (org-element-property :value export-snippet)))
858 ;;; Fixed Width
860 (defun org-e-texinfo-fixed-width (fixed-width contents info)
861 "Transcode a FIXED-WIDTH element from Org to Texinfo.
862 CONTENTS is nil. INFO is a plist holding contextual information."
863 (format "@example\n%s\n@end example"
864 (org-remove-indentation
865 (org-e-texinfo--sanitize-content
866 (org-element-property :value fixed-width)))))
868 ;;; Footnote Reference
871 (defun org-e-texinfo-footnote-reference (footnote contents info)
872 "Create a footnote reference for FOOTNOTE.
874 FOOTNOTE is the footnote to define. CONTENTS is nil. INFO is a
875 plist holding contextual information."
876 (let ((def (org-export-get-footnote-definition footnote info)))
877 (format "@footnote{%s}"
878 (org-trim (org-export-data def info)))))
880 ;;; Headline
882 (defun org-e-texinfo-headline (headline contents info)
883 "Transcode an HEADLINE element from Org to Texinfo.
884 CONTENTS holds the contents of the headline. INFO is a plist
885 holding contextual information."
886 (let* ((class (plist-get info :texinfo-class))
887 (level (org-export-get-relative-level headline info))
888 (numberedp (org-export-numbered-headline-p headline info))
889 (class-sectionning (assoc class org-e-texinfo-classes))
890 ;; Find the index type, if any
891 (index (org-element-property :index headline))
892 ;; Retrieve headline text
893 (text (org-e-texinfo--sanitize-headline
894 (org-element-property :title headline) info))
895 ;; Create node info, to insert it before section formatting.
896 (node (format "@node %s\n"
897 (org-e-texinfo--sanitize-menu
898 (replace-regexp-in-string "%" "%%" text))))
899 ;; Menus must be generated with first child, otherwise they
900 ;; will not nest properly
901 (menu (let* ((first (org-export-first-sibling-p headline info))
902 (parent (org-export-get-parent-headline headline))
903 (title (org-e-texinfo--sanitize-headline
904 (org-element-property :title parent) info))
905 heading listing
906 (tree (plist-get info :parse-tree)))
907 (if first
908 (org-element-map
909 (plist-get info :parse-tree) 'headline
910 (lambda (ref)
911 (if (member title (org-element-property :title ref))
912 (push ref heading)))
913 info 't))
914 (setq listing (org-e-texinfo--build-menu
915 (car heading) level info))
916 (if listing
917 (setq listing (replace-regexp-in-string
918 "%" "%%" listing)
919 listing (format
920 "\n@menu\n%s\n@end menu\n\n" listing))
921 'nil)))
922 ;; Section formatting will set two placeholders: one for the
923 ;; title and the other for the contents.
924 (section-fmt
925 (let ((sec (if (and (symbolp (nth 2 class-sectionning))
926 (fboundp (nth 2 class-sectionning)))
927 (funcall (nth 2 class-sectionning) level numberedp)
928 (nth (1+ level) class-sectionning))))
929 (cond
930 ;; No section available for that LEVEL.
931 ((not sec) nil)
932 ;; Section format directly returned by a function.
933 ((stringp sec) sec)
934 ;; (numbered-section . unnumbered-section)
935 ((not (consp (cdr sec)))
936 ;; If an index, always unnumbered
937 (if index
938 (concat menu node (cdr sec) "\n%s")
939 ;; Otherwise number as needed.
940 (concat menu node
941 (funcall
942 (if numberedp #'car #'cdr) sec) "\n%s"))))))
943 (todo
944 (and (plist-get info :with-todo-keywords)
945 (let ((todo (org-element-property :todo-keyword headline)))
946 (and todo (org-export-data todo info)))))
947 (todo-type (and todo (org-element-property :todo-type headline)))
948 (tags (and (plist-get info :with-tags)
949 (org-export-get-tags headline info)))
950 (priority (and (plist-get info :with-priority)
951 (org-element-property :priority headline)))
952 ;; Create the headline text along with a no-tag version. The
953 ;; latter is required to remove tags from table of contents.
954 (full-text (org-e-texinfo--sanitize-content
955 (if (functionp org-e-texinfo-format-headline-function)
956 ;; User-defined formatting function.
957 (funcall org-e-texinfo-format-headline-function
958 todo todo-type priority text tags)
959 ;; Default formatting.
960 (concat
961 (when todo
962 (format "@strong{%s} " todo))
963 (when priority (format "@emph{#%s} " priority))
964 text
965 (when tags
966 (format ":%s:"
967 (mapconcat 'identity tags ":")))))))
968 (full-text-no-tag
969 (org-e-texinfo--sanitize-content
970 (if (functionp org-e-texinfo-format-headline-function)
971 ;; User-defined formatting function.
972 (funcall org-e-texinfo-format-headline-function
973 todo todo-type priority text nil)
974 ;; Default formatting.
975 (concat
976 (when todo (format "@strong{%s} " todo))
977 (when priority (format "@emph{#%c} " priority))
978 text))))
979 (pre-blanks
980 (make-string (org-element-property :pre-blank headline) 10)))
981 (cond
982 ;; Case 1: This is a footnote section: ignore it.
983 ((org-element-property :footnote-section-p headline) nil)
984 ;; Case 2: This is the `copying' section: ignore it
985 ;; This is used elsewhere.
986 ((org-element-property :copying headline) nil)
987 ;; Case 3: An index. If it matches one of the known indexes,
988 ;; print it as such following the contents, otherwise
989 ;; print the contents and leave the index up to the user.
990 (index
991 (format
992 section-fmt full-text
993 (concat pre-blanks contents "\n"
994 (if (member index '("cp" "fn" "ky" "pg" "tp" "vr"))
995 (concat "@printindex " index)))))
996 ;; Case 4: This is a deep sub-tree: export it as a list item.
997 ;; Also export as items headlines for which no section
998 ;; format has been found.
999 ((or (not section-fmt) (org-export-low-level-p headline info))
1000 ;; Build the real contents of the sub-tree.
1001 (let ((low-level-body
1002 (concat
1003 ;; If the headline is the first sibling, start a list.
1004 (when (org-export-first-sibling-p headline info)
1005 (format "@%s\n" (if numberedp 'enumerate 'itemize)))
1006 ;; Itemize headline
1007 "@item\n" full-text "\n" pre-blanks contents)))
1008 ;; If headline is not the last sibling simply return
1009 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1010 ;; blank line.
1011 (if (not (org-export-last-sibling-p headline info)) low-level-body
1012 (replace-regexp-in-string
1013 "[ \t\n]*\\'"
1014 (format "\n@end %s" (if numberedp 'enumerate 'itemize))
1015 low-level-body))))
1016 ;; Case 5: Standard headline. Export it as a section.
1018 (cond
1019 ((not (and tags (eq (plist-get info :with-tags) 'not-in-toc)))
1020 ;; Regular section. Use specified format string.
1021 (format (replace-regexp-in-string "%]" "%%]" section-fmt) full-text
1022 (concat pre-blanks contents)))
1023 ((string-match "\\`@\\(.*?\\){" section-fmt)
1024 ;; If tags should be removed from table of contents, insert
1025 ;; title without tags as an alternative heading in sectioning
1026 ;; command.
1027 (format (replace-match (concat (match-string 1 section-fmt) "[%s]")
1028 nil nil section-fmt 1)
1029 ;; Replace square brackets with parenthesis since
1030 ;; square brackets are not supported in optional
1031 ;; arguments.
1032 (replace-regexp-in-string
1033 "\\[" "("
1034 (replace-regexp-in-string
1035 "\\]" ")"
1036 full-text-no-tag))
1037 full-text
1038 (concat pre-blanks contents)))
1040 ;; Impossible to add an alternative heading. Fallback to
1041 ;; regular sectioning format string.
1042 (format (replace-regexp-in-string "%]" "%%]" section-fmt) full-text
1043 (concat pre-blanks contents))))))))
1045 ;;; Inline Src Block
1047 (defun org-e-texinfo-inline-src-block (inline-src-block contents info)
1048 "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
1049 CONTENTS holds the contents of the item. INFO is a plist holding
1050 contextual information."
1051 (let* ((code (org-element-property :value inline-src-block))
1052 (separator (org-e-texinfo--find-verb-separator code)))
1053 (concat "@verb{" separator code separator "}")))
1055 ;;; Inlinetask
1057 (defun org-e-texinfo-inlinetask (inlinetask contents info)
1058 "Transcode an INLINETASK element from Org to Texinfo.
1059 CONTENTS holds the contents of the block. INFO is a plist
1060 holding contextual information."
1061 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1062 (todo (and (plist-get info :with-todo-keywords)
1063 (let ((todo (org-element-property :todo-keyword inlinetask)))
1064 (and todo (org-export-data todo info)))))
1065 (todo-type (org-element-property :todo-type inlinetask))
1066 (tags (and (plist-get info :with-tags)
1067 (org-export-get-tags inlinetask info)))
1068 (priority (and (plist-get info :with-priority)
1069 (org-element-property :priority inlinetask))))
1070 ;; If `org-e-texinfo-format-inlinetask-function' is provided, call it
1071 ;; with appropriate arguments.
1072 (if (functionp org-e-texinfo-format-inlinetask-function)
1073 (funcall org-e-texinfo-format-inlinetask-function
1074 todo todo-type priority title tags contents)
1075 ;; Otherwise, use a default template.
1076 (let ((full-title
1077 (concat
1078 (when todo (format "@strong{%s} " todo))
1079 (when priority (format "#%c " priority))
1080 title
1081 (when tags (format ":%s:"
1082 (mapconcat 'identity tags ":"))))))
1083 (format (concat "@center %s\n\n"
1084 "%s"
1085 "\n")
1086 full-title contents)))))
1088 ;;; Italic
1090 (defun org-e-texinfo-italic (italic contents info)
1091 "Transcode ITALIC from Org to Texinfo.
1092 CONTENTS is the text with italic markup. INFO is a plist holding
1093 contextual information."
1094 (org-e-texinfo--text-markup contents 'italic))
1096 ;;; Item
1098 (defun org-e-texinfo-item (item contents info)
1099 "Transcode an ITEM element from Org to Texinfo.
1100 CONTENTS holds the contents of the item. INFO is a plist holding
1101 contextual information."
1102 (let* ((tag (org-element-property :tag item))
1103 (desc (org-export-data tag info)))
1104 (concat "\n@item " (if tag desc) "\n"
1105 (org-trim contents) "\n")))
1107 ;;; Keyword
1109 (defun org-e-texinfo-keyword (keyword contents info)
1110 "Transcode a KEYWORD element from Org to Texinfo.
1111 CONTENTS is nil. INFO is a plist holding contextual information."
1112 (let ((key (org-element-property :key keyword))
1113 (value (org-element-property :value keyword)))
1114 (cond
1115 ((string= key "TEXINFO") value)
1116 ((string= key "CINDEX") (format "@cindex %s" value))
1117 ((string= key "FINDEX") (format "@findex %s" value))
1118 ((string= key "KINDEX") (format "@kindex %s" value))
1119 ((string= key "PINDEX") (format "@pindex %s" value))
1120 ((string= key "TINDEX") (format "@tindex %s" value))
1121 ((string= key "VINDEX") (format "@vindex %s" value)))))
1123 ;;; Line Break
1125 (defun org-e-texinfo-line-break (line-break contents info)
1126 "Transcode a LINE-BREAK object from Org to Texinfo.
1127 CONTENTS is nil. INFO is a plist holding contextual information."
1128 "@*")
1130 ;;; Link
1132 (defun org-e-texinfo-link (link desc info)
1133 "Transcode a LINK object from Org to Texinfo.
1135 DESC is the description part of the link, or the empty string.
1136 INFO is a plist holding contextual information. See
1137 `org-export-data'."
1138 (let* ((type (org-element-property :type link))
1139 (raw-path (org-element-property :path link))
1140 ;; Ensure DESC really exists, or set it to nil.
1141 (desc (and (not (string= desc "")) desc))
1142 (path (cond
1143 ((member type '("http" "https" "ftp"))
1144 (concat type ":" raw-path))
1145 ((string= type "file")
1146 (if (file-name-absolute-p raw-path)
1147 (concat "file://" (expand-file-name raw-path))
1148 (concat "file://" raw-path)))
1149 (t raw-path)))
1150 (email (if (string= type "mailto")
1151 (let ((text (replace-regexp-in-string
1152 "@" "@@" raw-path)))
1153 (concat text (if desc (concat "," desc))))))
1154 protocol)
1155 (cond
1156 ;; Links pointing to an headline: Find destination and build
1157 ;; appropriate referencing command.
1158 ((member type '("custom-id" "id"))
1159 (let ((destination (org-export-resolve-id-link link info)))
1160 (case (org-element-type destination)
1161 ;; Id link points to an external file.
1162 (plain-text
1163 (if desc (format "@uref{file://%s,%s}" destination desc)
1164 (format "@uref{file://%s}" destination)))
1165 ;; LINK points to an headline. Use the headline as the NODE target
1166 (headline
1167 (format "@ref{%s}"
1168 (org-export-data
1169 (org-element-property :title destination) info)))
1170 (otherwise
1171 (let ((path (org-export-solidify-link-text path)))
1172 (if (not desc) (format "@ref{%s}" path)
1173 (format "@ref{%s,,%s}" path desc)))))))
1174 ((member type '("fuzzy"))
1175 (let ((destination (org-export-resolve-fuzzy-link link info)))
1176 (case (org-element-type destination)
1177 ;; Id link points to an external file.
1178 (plain-text
1179 (if desc (format "@uref{file://%s,%s}" destination desc)
1180 (format "@uref{file://%s}" destination)))
1181 ;; LINK points to an headline. Use the headline as the NODE target
1182 (headline
1183 (format "@ref{%s}"
1184 (org-export-data
1185 (org-element-property :title destination) info)))
1186 (otherwise
1187 (let ((path (org-export-solidify-link-text path)))
1188 (if (not desc) (format "@ref{%s}" path)
1189 (format "@ref{%s,,%s}" path desc)))))))
1190 ;; Special case for email addresses
1191 (email
1192 (format "@email{%s}" email))
1193 ;; External link with a description part.
1194 ((and path desc) (format "@uref{%s,%s}" path desc))
1195 ;; External link without a description part.
1196 (path (format "@uref{%s}" path))
1197 ;; No path, only description. Try to do something useful.
1198 (t (format org-e-texinfo-link-with-unknown-path-format desc)))))
1201 ;;; Menu
1203 (defun org-e-texinfo-make-menu (info level)
1204 "Create the menu for inclusion in the texifo document.
1206 INFO is the parsed buffer that contains the headlines. LEVEL
1207 determines whether to make the main menu, or the detailed menu.
1209 This is only used for generating the primary menu. In-Node menus
1210 are generated directly."
1211 (let* ((parse (plist-get info :parse-tree))
1212 ;; Top determines level to build menu from, it finds the
1213 ;; level of the first headline in the export.
1214 (top (org-element-map
1215 parse 'headline
1216 (lambda (headline)
1217 (org-element-property :level headline)) info 't)))
1218 (cond
1219 ;; Generate the main menu
1220 ((eq level 'main)
1221 (org-e-texinfo--build-menu parse top info))
1222 ;; Generate the detailed (recursive) menu
1223 ((eq level 'detailed)
1224 ;; Requires recursion
1225 ;;(org-e-texinfo--build-detailed-menu parse top info)
1226 (org-e-texinfo--build-menu parse top info 'detailed))
1227 ;; Otherwise do nothing
1228 (t))))
1230 ;;; Paragraph
1232 (defun org-e-texinfo-paragraph (paragraph contents info)
1233 "Transcode a PARAGRAPH element from Org to Texinfo.
1234 CONTENTS is the contents of the paragraph, as a string. INFO is
1235 the plist used as a communication channel."
1236 contents)
1238 ;;; Plain List
1240 (defun org-e-texinfo-plain-list (plain-list contents info)
1241 "Transcode a PLAIN-LIST element from Org to Texinfo.
1242 CONTENTS is the contents of the list. INFO is a plist holding
1243 contextual information."
1244 (let* ((attr (org-export-read-attribute :attr_texinfo plain-list))
1245 (indic (or (plist-get attr :indic)
1246 org-e-texinfo-def-table-markup))
1247 (type (org-element-property :type plain-list))
1248 (table-type (or (plist-get attr :table-type)
1249 "table"))
1250 ;; Ensure valid texinfo table type.
1251 (table-type (if (memq table-type '("table" "ftable" "vtable"))
1252 table-type
1253 "table"))
1254 (list-type (cond
1255 ((eq type 'ordered) "enumerate")
1256 ((eq type 'unordered) "itemize")
1257 ((eq type 'descriptive) table-type))))
1258 (format "@%s%s\n@end %s"
1259 (if (eq type 'descriptive)
1260 (concat list-type " " indic)
1261 list-type)
1262 contents
1263 list-type)))
1265 ;;; Plain Text
1267 (defun org-e-texinfo-plain-text (text info)
1268 "Transcode a TEXT string from Org to Texinfo.
1269 TEXT is the string to transcode. INFO is a plist holding
1270 contextual information."
1271 ;; First protect @, { and }.
1272 (let ((output (org-e-texinfo--sanitize-content text)))
1273 ;; Activate smart quotes. Be sure to provide original TEXT string
1274 ;; since OUTPUT may have been modified.
1275 (when (plist-get info :with-smart-quotes)
1276 (setq output
1277 (org-export-activate-smart-quotes output :texinfo info text)))
1278 ;; LaTeX into @LaTeX{} and TeX into @TeX{}
1279 (let ((case-fold-search nil)
1280 (start 0))
1281 (while (string-match "\\(\\(?:La\\)?TeX\\)" output start)
1282 (setq output (replace-match
1283 (format "@%s{}" (match-string 1 output)) nil t output)
1284 start (match-end 0))))
1285 ;; Convert special strings.
1286 (when (plist-get info :with-special-strings)
1287 (while (string-match (regexp-quote "...") output)
1288 (setq output (replace-match "@dots{}" nil t output))))
1289 ;; Handle break preservation if required.
1290 (when (plist-get info :preserve-breaks)
1291 (setq output (replace-regexp-in-string
1292 "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" output)))
1293 ;; Return value.
1294 output))
1296 ;;; Planning
1298 (defun org-e-texinfo-planning (planning contents info)
1299 "Transcode a PLANNING element from Org to Texinfo.
1300 CONTENTS is nil. INFO is a plist holding contextual
1301 information."
1302 (concat
1303 "@noindent"
1304 (mapconcat
1305 'identity
1306 (delq nil
1307 (list
1308 (let ((closed (org-element-property :closed planning)))
1309 (when closed
1310 (concat
1311 (format "@strong%s} " org-closed-string)
1312 (format org-e-texinfo-inactive-timestamp-format
1313 (org-translate-time closed)))))
1314 (let ((deadline (org-element-property :deadline planning)))
1315 (when deadline
1316 (concat
1317 (format "@strong{%s} " org-deadline-string)
1318 (format org-e-texinfo-active-timestamp-format
1319 (org-translate-time deadline)))))
1320 (let ((scheduled (org-element-property :scheduled planning)))
1321 (when scheduled
1322 (concat
1323 (format "@strong{%s} " org-scheduled-string)
1324 (format org-e-texinfo-active-timestamp-format
1325 (org-translate-time scheduled)))))))
1326 " ")
1327 "@*"))
1329 ;;; Property Drawer
1331 (defun org-e-texinfo-property-drawer (property-drawer contents info)
1332 "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
1333 CONTENTS is nil. INFO is a plist holding contextual
1334 information."
1335 ;; The property drawer isn't exported but we want separating blank
1336 ;; lines nonetheless.
1339 ;;; Quote Block
1341 (defun org-e-texinfo-quote-block (quote-block contents info)
1342 "Transcode a QUOTE-BLOCK element from Org to Texinfo.
1343 CONTENTS holds the contents of the block. INFO is a plist
1344 holding contextual information."
1345 (let* ((title (org-element-property :name quote-block))
1346 (start-quote (concat "@quotation"
1347 (if title
1348 (format " %s" title)))))
1349 (format "%s\n%s@end quotation" start-quote contents)))
1351 ;;; Quote Section
1353 (defun org-e-texinfo-quote-section (quote-section contents info)
1354 "Transcode a QUOTE-SECTION element from Org to Texinfo.
1355 CONTENTS is nil. INFO is a plist holding contextual information."
1356 (let ((value (org-remove-indentation
1357 (org-element-property :value quote-section))))
1358 (when value (format "@verbatim\n%s@end verbatim" value))))
1360 ;;; Radio Target
1362 (defun org-e-texinfo-radio-target (radio-target text info)
1363 "Transcode a RADIO-TARGET object from Org to Texinfo.
1364 TEXT is the text of the target. INFO is a plist holding
1365 contextual information."
1366 (format "@anchor{%s}%s"
1367 (org-export-solidify-link-text
1368 (org-element-property :value radio-target))
1369 text))
1371 ;;; Section
1373 (defun org-e-texinfo-section (section contents info)
1374 "Transcode a SECTION element from Org to Texinfo.
1375 CONTENTS holds the contents of the section. INFO is a plist
1376 holding contextual information."
1377 contents)
1379 ;;; Special Block
1381 (defun org-e-texinfo-special-block (special-block contents info)
1382 "Transcode a SPECIAL-BLOCK element from Org to Texinfo.
1383 CONTENTS holds the contents of the block. INFO is a plist used
1384 as a communication channel."
1385 contents)
1387 ;;; Src Block
1389 (defun org-e-texinfo-src-block (src-block contents info)
1390 "Transcode a SRC-BLOCK element from Org to Texinfo.
1391 CONTENTS holds the contents of the item. INFO is a plist holding
1392 contextual information."
1393 (let* ((lang (org-element-property :language src-block))
1394 (lisp-p (string-match-p "lisp" lang)))
1395 (cond
1396 ;; Case 1. Lisp Block
1397 (lisp-p
1398 (format "@lisp\n%s\n@end lisp"
1399 (org-export-format-code-default src-block info)))
1400 ;; Case 2. Other blocks
1402 (format "@example\n%s\n@end example"
1403 (org-export-format-code-default src-block info))))))
1405 ;;; Statistics Cookie
1407 (defun org-e-texinfo-statistics-cookie (statistics-cookie contents info)
1408 "Transcode a STATISTICS-COOKIE object from Org to Texinfo.
1409 CONTENTS is nil. INFO is a plist holding contextual information."
1410 (org-element-property :value statistics-cookie))
1412 ;;; Subscript
1414 (defun org-e-texinfo-subscript (subscript contents info)
1415 "Transcode a SUBSCRIPT object from Org to Texinfo.
1416 CONTENTS is the contents of the object. INFO is a plist holding
1417 contextual information."
1418 (format "@math{_%s}" contents))
1420 ;;; Superscript
1422 (defun org-e-texinfo-superscript (superscript contents info)
1423 "Transcode a SUPERSCRIPT object from Org to Texinfo.
1424 CONTENTS is the contents of the object. INFO is a plist holding
1425 contextual information."
1426 (format "@math{^%s}" contents))
1428 ;;; Table
1430 ;; `org-e-texinfo-table' is the entry point for table transcoding. It
1431 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
1432 ;; delegates the job to either `org-e-texinfo-table--table.el-table' or
1433 ;; `org-e-texinfo-table--org-table' functions, depending of the type of
1434 ;; the table.
1436 ;; `org-e-texinfo-table--align-string' is a subroutine used to build
1437 ;; alignment string for Org tables.
1439 (defun org-e-texinfo-table (table contents info)
1440 "Transcode a TABLE element from Org to Texinfo.
1441 CONTENTS is the contents of the table. INFO is a plist holding
1442 contextual information."
1443 (cond
1444 ;; Case 1: verbatim table.
1445 ((or org-e-texinfo-tables-verbatim
1446 (let ((attr (mapconcat 'identity
1447 (org-element-property :attr_latex table)
1448 " ")))
1449 (and attr (string-match "\\<verbatim\\>" attr))))
1450 (format "@verbatim \n%s\n@end verbatim"
1451 ;; Re-create table, without affiliated keywords.
1452 (org-trim
1453 (org-element-interpret-data
1454 `(table nil ,@(org-element-contents table))))))
1455 ;; Case 2: table.el table. Convert it using appropriate tools.
1456 ((eq (org-element-property :type table) 'table.el)
1457 (org-e-texinfo-table--table.el-table table contents info))
1458 ;; Case 3: Standard table.
1459 (t (org-e-texinfo-table--org-table table contents info))))
1461 (defun org-e-texinfo-table-column-widths (table info)
1462 "Determine the largest table cell in each column to process alignment.
1464 TABLE is the table element to transcode. INFO is a plist used as
1465 a communication channel."
1466 (let* ((rows (org-element-map table 'table-row 'identity info))
1467 (collected (loop for row in rows collect
1468 (org-element-map
1469 row 'table-cell 'identity info)))
1470 (number-cells (length (car collected)))
1471 cells counts)
1472 (loop for row in collected do
1473 (push (mapcar (lambda (ref)
1474 (let* ((start (org-element-property :contents-begin ref))
1475 (end (org-element-property :contents-end ref))
1476 (length (- end start)))
1477 length)) row) cells))
1478 (setq cells (org-remove-if 'null cells))
1479 (push (loop for count from 0 to (- number-cells 1) collect
1480 (loop for item in cells collect
1481 (nth count item))) counts)
1482 (mapconcat (lambda (size)
1483 (make-string size ?a)) (mapcar (lambda (ref)
1484 (apply 'max `,@ref)) (car counts))
1485 "} {")))
1487 (defun org-e-texinfo-table--org-table (table contents info)
1488 "Return appropriate Texinfo code for an Org table.
1490 TABLE is the table type element to transcode. CONTENTS is its
1491 contents, as a string. INFO is a plist used as a communication
1492 channel.
1494 This function assumes TABLE has `org' as its `:type' attribute."
1495 (let* ((attr (org-export-read-attribute :attr_texinfo table))
1496 (col-width (plist-get attr :columns))
1497 (columns (if col-width
1498 (format "@columnfractions %s"
1499 col-width)
1500 (format "{%s}"
1501 (org-e-texinfo-table-column-widths
1502 table info)))))
1503 ;; Prepare the final format string for the table.
1504 (cond
1505 ;; Longtable.
1506 ;; Others.
1507 (t (concat
1508 (format "@multitable %s\n%s@end multitable"
1509 columns
1510 contents))))))
1512 (defun org-e-texinfo-table--table.el-table (table contents info)
1513 "Returns nothing.
1515 Rather than return an invalid table, nothing is returned."
1516 'nil)
1518 ;;; Table Cell
1520 (defun org-e-texinfo-table-cell (table-cell contents info)
1521 "Transcode a TABLE-CELL element from Org to Texinfo.
1522 CONTENTS is the cell contents. INFO is a plist used as
1523 a communication channel."
1524 (concat (if (and contents
1525 org-e-texinfo-table-scientific-notation
1526 (string-match orgtbl-exp-regexp contents))
1527 ;; Use appropriate format string for scientific
1528 ;; notation.
1529 (format org-e-texinfo-table-scientific-notation
1530 (match-string 1 contents)
1531 (match-string 2 contents))
1532 contents)
1533 (when (org-export-get-next-element table-cell info) "\n@tab ")))
1535 ;;; Table Row
1537 (defun org-e-texinfo-table-row (table-row contents info)
1538 "Transcode a TABLE-ROW element from Org to Texinfo.
1539 CONTENTS is the contents of the row. INFO is a plist used as
1540 a communication channel."
1541 ;; Rules are ignored since table separators are deduced from
1542 ;; borders of the current row.
1543 (when (eq (org-element-property :type table-row) 'standard)
1544 (let ((rowgroup-tag
1545 (cond
1546 ;; Case 1: Belongs to second or subsequent rowgroup.
1547 ((not (= 1 (org-export-table-row-group table-row info)))
1548 "@item ")
1549 ;; Case 2: Row is from first rowgroup. Table has >=1 rowgroups.
1550 ((org-export-table-has-header-p
1551 (org-export-get-parent-table table-row) info)
1552 "@headitem ")
1553 ;; Case 3: Row is from first and only row group.
1554 (t "@item "))))
1555 (when (eq (org-element-property :type table-row) 'standard)
1556 (concat rowgroup-tag contents "\n")))))
1558 ;;; Target
1560 (defun org-e-texinfo-target (target contents info)
1561 "Transcode a TARGET object from Org to Texinfo.
1562 CONTENTS is nil. INFO is a plist holding contextual
1563 information."
1564 (format "@anchor{%s}"
1565 (org-export-solidify-link-text (org-element-property :value target))))
1567 ;;; Timestamp
1569 (defun org-e-texinfo-timestamp (timestamp contents info)
1570 "Transcode a TIMESTAMP object from Org to Texinfo.
1571 CONTENTS is nil. INFO is a plist holding contextual
1572 information."
1573 (let ((value (org-translate-time
1574 (org-element-property :raw-value timestamp))))
1575 (case (org-element-property :type timestamp)
1576 ((active active-range)
1577 (format org-e-texinfo-active-timestamp-format value))
1578 ((inactive inactive-range)
1579 (format org-e-texinfo-inactive-timestamp-format value))
1580 (t (format org-e-texinfo-diary-timestamp-format value)))))
1582 ;;; Verbatim
1584 (defun org-e-texinfo-verbatim (verbatim contents info)
1585 "Transcode a VERBATIM object from Org to Texinfo.
1586 CONTENTS is nil. INFO is a plist used as a communication
1587 channel."
1588 (org-e-texinfo--text-markup (org-element-property :value verbatim) 'verbatim))
1590 ;;; Verse Block
1592 (defun org-e-texinfo-verse-block (verse-block contents info)
1593 "Transcode a VERSE-BLOCK element from Org to Texinfo.
1594 CONTENTS is verse block contents. INFO is a plist holding
1595 contextual information."
1596 ;; In a verse environment, add a line break to each newline
1597 ;; character and change each white space at beginning of a line
1598 ;; into a space of 1 em. Also change each blank line with
1599 ;; a vertical space of 1 em.
1600 (progn
1601 (setq contents (replace-regexp-in-string
1602 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
1603 (replace-regexp-in-string
1604 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
1605 (while (string-match "^[ \t]+" contents)
1606 (let ((new-str (format "\\hspace*{%dem}"
1607 (length (match-string 0 contents)))))
1608 (setq contents (replace-match new-str nil t contents))))
1609 (format "\\begin{verse}\n%s\\end{verse}" contents)))
1612 ;;; Interactive functions
1614 (defun org-e-texinfo-export-to-texinfo
1615 (&optional subtreep visible-only body-only ext-plist pub-dir)
1616 "Export current buffer to a Texinfo file.
1618 If narrowing is active in the current buffer, only export its
1619 narrowed part.
1621 If a region is active, export that region.
1623 When optional argument SUBTREEP is non-nil, export the sub-tree
1624 at point, extracting information from the headline properties
1625 first.
1627 When optional argument VISIBLE-ONLY is non-nil, don't export
1628 contents of hidden elements.
1630 When optional argument BODY-ONLY is non-nil, only write code
1631 between \"\\begin{document}\" and \"\\end{document}\".
1633 EXT-PLIST, when provided, is a property list with external
1634 parameters overriding Org default settings, but still inferior to
1635 file-local settings.
1637 When optional argument PUB-DIR is set, use it as the publishing
1638 directory.
1640 Return output file's name."
1641 (interactive)
1642 (let ((outfile (org-export-output-file-name ".texi" subtreep pub-dir)))
1643 (org-export-to-file
1644 'e-texinfo outfile subtreep visible-only body-only ext-plist)))
1646 (defun org-e-texinfo-export-to-info
1647 (&optional subtreep visible-only body-only ext-plist pub-dir)
1648 "Export current buffer to Texinfo then process through to INFO.
1650 If narrowing is active in the current buffer, only export its
1651 narrowed part.
1653 If a region is active, export that region.
1655 When optional argument SUBTREEP is non-nil, export the sub-tree
1656 at point, extracting information from the headline properties
1657 first.
1659 When optional argument VISIBLE-ONLY is non-nil, don't export
1660 contents of hidden elements.
1662 When optional argument BODY-ONLY is non-nil, only write code
1663 between \"\\begin{document}\" and \"\\end{document}\".
1665 EXT-PLIST, when provided, is a property list with external
1666 parameters overriding Org default settings, but still inferior to
1667 file-local settings.
1669 When optional argument PUB-DIR is set, use it as the publishing
1670 directory.
1672 Return INFO file's name."
1673 (interactive)
1674 (org-e-texinfo-compile
1675 (org-e-texinfo-export-to-texinfo
1676 subtreep visible-only body-only ext-plist pub-dir)))
1678 (defun org-e-texinfo-compile (file)
1679 "Compile a texinfo file.
1681 FILE is the name of the file being compiled. Processing is
1682 done through the command specified in `org-e-texinfo-info-process'.
1684 Return INFO file name or an error if it couldn't be produced."
1685 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1686 (full-name (file-truename file))
1687 (out-dir (file-name-directory file))
1688 ;; Make sure `default-directory' is set to FILE directory,
1689 ;; not to whatever value the current buffer may have.
1690 (default-directory (file-name-directory full-name))
1691 errors)
1692 (message (format "Processing Texinfo file %s ..." file))
1693 (save-window-excursion
1694 (cond
1695 ;; A function is provided: Apply it.
1696 ((functionp org-e-texinfo-info-process)
1697 (funcall org-e-texinfo-info-process (shell-quote-argument file)))
1698 ;; A list is provided: Replace %b, %f and %o with appropriate
1699 ;; values in each command before applying it. Output is
1700 ;; redirected to "*Org INFO Texinfo Output*" buffer.
1701 ((consp org-e-texinfo-info-process)
1702 (let ((outbuf (get-buffer-create "*Org INFO Texinfo Output*")))
1703 (mapc
1704 (lambda (command)
1705 (shell-command
1706 (replace-regexp-in-string
1707 "%b" (shell-quote-argument base-name)
1708 (replace-regexp-in-string
1709 "%f" (shell-quote-argument full-name)
1710 (replace-regexp-in-string
1711 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1712 outbuf))
1713 org-e-texinfo-info-process)
1714 ;; Collect standard errors from output buffer.
1715 (setq errors (org-e-texinfo-collect-errors outbuf))))
1716 (t (error "No valid command to process to Info")))
1717 (let ((infofile (concat out-dir base-name ".info")))
1718 ;; Check for process failure. Provide collected errors if
1719 ;; possible.
1720 (if (not (file-exists-p infofile))
1721 (error (concat (format "INFO file %s wasn't produced" infofile)
1722 (when errors (concat ": " errors))))
1723 ;; Else remove log files, when specified, and signal end of
1724 ;; process to user, along with any error encountered.
1725 (message (concat "Process completed"
1726 (if (not errors) "."
1727 (concat " with errors: " errors)))))
1728 ;; Return output file name.
1729 infofile))))
1731 (defun org-e-texinfo-collect-errors (buffer)
1732 "Collect some kind of errors from \"makeinfo\" command output.
1734 BUFFER is the buffer containing output.
1736 Return collected error types as a string, or nil if there was
1737 none."
1738 (with-current-buffer buffer
1739 (save-excursion
1740 (goto-char (point-min))
1741 ;; Find final "makeinfo" run.
1742 (when t
1743 (let ((case-fold-search t)
1744 (errors ""))
1745 (when (save-excursion
1746 (re-search-forward "perhaps incorrect sectioning?" nil t))
1747 (setq errors (concat errors " [incorrect sectionnng]")))
1748 (when (save-excursion
1749 (re-search-forward "missing close brace" nil t))
1750 (setq errors (concat errors " [syntax error]")))
1751 (when (save-excursion
1752 (re-search-forward "Unknown command" nil t))
1753 (setq errors (concat errors " [undefined @command]")))
1754 (when (save-excursion
1755 (re-search-forward "No matching @end" nil t))
1756 (setq errors (concat errors " [block incomplete]")))
1757 (when (save-excursion
1758 (re-search-forward "requires a sectioning" nil t))
1759 (setq errors (concat errors " [invalid section command]")))
1760 (when (save-excursion
1761 (re-search-forward "\\[unexpected\]" nil t))
1762 (setq errors (concat errors " [unexpected error]")))
1763 (when (save-excursion
1764 (re-search-forward "misplaced " nil t))
1765 (setq errors (concat errors " [syntax error]")))
1766 (and (org-string-nw-p errors) (org-trim errors)))))))
1768 (provide 'org-e-texinfo)
1769 ;;; org-e-texinfo.el ends here