org-e-groff, org-e-man, org-e-texinfo: Silence byte-compiler
[org-mode.git] / contrib / lisp / org-e-texinfo.el
blob02513462fd16afa99f178bfadcdab96489d7eeae
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 (horizontal-rule . org-e-texinfo-horizontal-rule)
86 (inline-src-block . org-e-texinfo-inline-src-block)
87 (inlinetask . org-e-texinfo-inlinetask)
88 (italic . org-e-texinfo-italic)
89 (item . org-e-texinfo-item)
90 (keyword . org-e-texinfo-keyword)
91 (latex-environment . org-e-texinfo-latex-environment)
92 (latex-fragment . org-e-texinfo-latex-fragment)
93 (line-break . org-e-texinfo-line-break)
94 (link . org-e-texinfo-link)
95 (paragraph . org-e-texinfo-paragraph)
96 (plain-list . org-e-texinfo-plain-list)
97 (plain-text . org-e-texinfo-plain-text)
98 (planning . org-e-texinfo-planning)
99 (property-drawer . org-e-texinfo-property-drawer)
100 (quote-block . org-e-texinfo-quote-block)
101 (quote-section . org-e-texinfo-quote-section)
102 (radio-target . org-e-texinfo-radio-target)
103 (section . org-e-texinfo-section)
104 (special-block . org-e-texinfo-special-block)
105 (src-block . org-e-texinfo-src-block)
106 (statistics-cookie . org-e-texinfo-statistics-cookie)
107 (strike-through . org-e-texinfo-strike-through)
108 (subscript . org-e-texinfo-subscript)
109 (superscript . org-e-texinfo-superscript)
110 (table . org-e-texinfo-table)
111 (table-cell . org-e-texinfo-table-cell)
112 (table-row . org-e-texinfo-table-row)
113 (target . org-e-texinfo-target)
114 (template . org-e-texinfo-template)
115 (timestamp . org-e-texinfo-timestamp)
116 (underline . org-e-texinfo-underline)
117 (verbatim . org-e-texinfo-verbatim)
118 (verse-block . org-e-texinfo-verse-block))
119 :export-block "TEXINFO"
120 :filters-alist
121 ((:filter-headline . org-e-texinfo-filter-section-blank-lines)
122 (:filter-section . org-e-texinfo-filter-section-blank-lines))
123 :menu-entry
124 (?i "Export to Texinfo"
125 ((?t "As TEXI file" org-e-texinfo-export-to-texinfo)
126 (?i "As INFO file" org-e-texinfo-export-to-info)))
127 :options-alist
128 ((:texinfo-filename "TEXINFO_FILENAME" nil org-e-texinfo-filename t)
129 (:texinfo-class "TEXINFO_CLASS" nil org-e-texinfo-default-class t)
130 (:texinfo-header "TEXINFO_HEADER" nil nil newline)
131 (:subtitle "SUBTITLE" nil nil newline)
132 (:subauthor "SUBAUTHOR" nil nil newline)
133 (:texinfo-dircat "TEXINFO_DIR_CATEGORY" nil nil t)
134 (:texinfo-dirtitle "TEXINFO_DIR_TITLE" nil nil t)
135 (:texinfo-dirdesc "TEXINFO_DIR_DESC" nil nil t)))
139 ;;; User Configurable Variables
141 (defgroup org-export-e-texinfo nil
142 "Options for exporting Org mode files to Texinfo."
143 :tag "Org Export Texinfo"
144 :group 'org-export)
146 ;;; Preamble
148 (defcustom org-e-texinfo-filename nil
149 "Default filename for texinfo output."
150 :group 'org-export-e-texinfo
151 :type '(string :tag "Export Filename"))
153 (defcustom org-e-texinfo-default-class "info"
154 "The default Texinfo class."
155 :group 'org-export-e-texinfo
156 :type '(string :tag "Texinfo class"))
158 (defcustom org-e-texinfo-classes
159 '(("info"
160 "\\input texinfo @c -*- texinfo -*-"
161 ("@chapter %s" . "@unnumbered %s")
162 ("@section %s" . "@unnumberedsec %s")
163 ("@subsection %s" . "@unnumberedsubsec %s")
164 ("@subsubsection %s" . "@unnumberedsubsubsec %s")))
165 "Alist of Texinfo classes and associated header and structure.
166 If #+Texinfo_CLASS is set in the buffer, use its value and the
167 associated information. Here is the structure of each cell:
169 \(class-name
170 header-string
171 \(numbered-section . unnumbered-section\)
172 ...\)
174 The sectioning structure
175 ------------------------
177 The sectioning structure of the class is given by the elements
178 following the header string. For each sectioning level, a number
179 of strings is specified. A %s formatter is mandatory in each
180 section string and will be replaced by the title of the section.
182 Instead of a list of sectioning commands, you can also specify
183 a function name. That function will be called with two
184 parameters, the \(reduced) level of the headline, and a predicate
185 non-nil when the headline should be numbered. It must return
186 a format string in which the section title will be added."
187 :group 'org-export-e-texinfo
188 :type '(repeat
189 (list (string :tag "Texinfo class")
190 (string :tag "Texinfo header")
191 (repeat :tag "Levels" :inline t
192 (choice
193 (cons :tag "Heading"
194 (string :tag " numbered")
195 (string :tag "unnumbered"))
196 (function :tag "Hook computing sectioning"))))))
198 ;;; Headline
200 (defcustom org-e-texinfo-format-headline-function nil
201 "Function to format headline text.
203 This function will be called with 5 arguments:
204 TODO the todo keyword (string or nil).
205 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
206 PRIORITY the priority of the headline (integer or nil)
207 TEXT the main headline text (string).
208 TAGS the tags as a list of strings (list of strings or nil).
210 The function result will be used in the section format string.
212 As an example, one could set the variable to the following, in
213 order to reproduce the default set-up:
215 \(defun org-e-texinfo-format-headline (todo todo-type priority text tags)
216 \"Default format function for an headline.\"
217 \(concat (when todo
218 \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo))
219 \(when priority
220 \(format \"\\\\framebox{\\\\#%c} \" priority))
221 text
222 \(when tags
223 \(format \"\\\\hfill{}\\\\textsc{%s}\"
224 \(mapconcat 'identity tags \":\"))))"
225 :group 'org-export-e-texinfo
226 :type 'function)
229 ;;; Footnotes
231 ;; Footnotes are inserted directly
233 ;;; Timestamps
235 (defcustom org-e-texinfo-active-timestamp-format "@emph{%s}"
236 "A printf format string to be applied to active timestamps."
237 :group 'org-export-e-texinfo
238 :type 'string)
240 (defcustom org-e-texinfo-inactive-timestamp-format "@emph{%s}"
241 "A printf format string to be applied to inactive timestamps."
242 :group 'org-export-e-texinfo
243 :type 'string)
245 (defcustom org-e-texinfo-diary-timestamp-format "@emph{%s}"
246 "A printf format string to be applied to diary timestamps."
247 :group 'org-export-e-texinfo
248 :type 'string)
250 ;;; Links
252 (defcustom org-e-texinfo-link-with-unknown-path-format "@indicateurl{%s}"
253 "Format string for links with unknown path type."
254 :group 'org-export-e-texinfo
255 :type 'string)
257 ;;; Tables
259 (defcustom org-e-texinfo-tables-verbatim nil
260 "When non-nil, tables are exported verbatim."
261 :group 'org-export-e-texinfo
262 :type 'boolean)
264 (defcustom org-e-texinfo-table-scientific-notation "%s\\,(%s)"
265 "Format string to display numbers in scientific notation.
266 The format should have \"%s\" twice, for mantissa and exponent
267 \(i.e. \"%s\\\\times10^{%s}\").
269 When nil, no transformation is made."
270 :group 'org-export-e-texinfo
271 :type '(choice
272 (string :tag "Format string")
273 (const :tag "No formatting")))
275 (defcustom org-e-texinfo-def-table-markup "@samp"
276 "Default setting for @table environments.")
278 ;;; Text markup
280 (defcustom org-e-texinfo-text-markup-alist '((bold . "@strong{%s}")
281 (code . code)
282 (italic . "@emph{%s}")
283 (verbatim . verb)
284 (comment . "@c %s"))
285 "Alist of Texinfo expressions to convert text markup.
287 The key must be a symbol among `bold', `italic' and `comment'.
288 The value is a formatting string to wrap fontified text with.
290 Value can also be set to the following symbols: `verb' and
291 `code'. For the former, Org will use \"@verb\" to
292 create a format string and select a delimiter character that
293 isn't in the string. For the latter, Org will use \"@code\"
294 to typeset and try to protect special characters.
296 If no association can be found for a given markup, text will be
297 returned as-is."
298 :group 'org-export-e-texinfo
299 :type 'alist
300 :options '(bold code italic verbatim comment))
302 ;;; Drawers
304 (defcustom org-e-texinfo-format-drawer-function nil
305 "Function called to format a drawer in Texinfo code.
307 The function must accept two parameters:
308 NAME the drawer name, like \"LOGBOOK\"
309 CONTENTS the contents of the drawer.
311 The function should return the string to be exported.
313 For example, the variable could be set to the following function
314 in order to mimic default behaviour:
316 \(defun org-e-texinfo-format-drawer-default \(name contents\)
317 \"Format a drawer element for Texinfo export.\"
318 contents\)"
319 :group 'org-export-e-texinfo
320 :type 'function)
322 ;;; Inlinetasks
324 (defcustom org-e-texinfo-format-inlinetask-function nil
325 "Function called to format an inlinetask in Texinfo code.
327 The function must accept six parameters:
328 TODO the todo keyword, as a string
329 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
330 PRIORITY the inlinetask priority, as a string
331 NAME the inlinetask name, as a string.
332 TAGS the inlinetask tags, as a list of strings.
333 CONTENTS the contents of the inlinetask, as a string.
335 The function should return the string to be exported.
337 For example, the variable could be set to the following function
338 in order to mimic default behaviour:
340 \(defun org-e-texinfo-format-inlinetask \(todo type priority name tags contents\)
341 \"Format an inline task element for Texinfo export.\"
342 \(let ((full-title
343 \(concat
344 \(when todo
345 \(format \"@strong{%s} \" todo))
346 \(when priority (format \"#%c \" priority))
347 title
348 \(when tags
349 \(format \":%s:\"
350 \(mapconcat 'identity tags \":\")))))
351 \(format (concat \"@center %s\n\n\"
352 \"%s\"
353 \"\n\"))
354 full-title contents))"
355 :group 'org-export-e-texinfo
356 :type 'function)
358 ;;; Src blocks
360 ;; Src Blocks are example blocks, except for LISP
362 ;;; Plain text
364 (defcustom org-e-texinfo-quotes
365 '(("quotes"
366 ("\\(\\s-\\|[[(]\\|^\\)\"" . "``")
367 ("\\(\\S-\\)\"" . "''")
368 ("\\(\\s-\\|(\\|^\\)'" . "`")))
369 "Alist for quotes to use when converting english double-quotes.
371 The CAR of each item in this alist is the language code.
372 The CDR of each item in this alist is a list of three CONS:
373 - the first CONS defines the opening quote;
374 - the second CONS defines the closing quote;
375 - the last CONS defines single quotes.
377 For each item in a CONS, the first string is a regexp
378 for allowed characters before/after the quote, the second
379 string defines the replacement string for this quote."
380 :group 'org-export-e-texinfo
381 :type '(list
382 (cons :tag "Opening quote"
383 (string :tag "Regexp for char before")
384 (string :tag "Replacement quote "))
385 (cons :tag "Closing quote"
386 (string :tag "Regexp for char after ")
387 (string :tag "Replacement quote "))
388 (cons :tag "Single quote"
389 (string :tag "Regexp for char before")
390 (string :tag "Replacement quote "))))
392 ;;; Compilation
394 (defcustom org-e-texinfo-info-process
395 '("makeinfo %f")
396 "Commands to process a texinfo file to an INFO file.
397 This is list of strings, each of them will be given to the shell
398 as a command. %f in the command will be replaced by the full
399 file name, %b by the file base name \(i.e without extension) and
400 %o by the base directory of the file."
401 :group 'org-export-texinfo
402 :type '(repeat :tag "Shell command sequence"
403 (string :tag "Shell command")))
406 ;;; Internal Functions
408 (defun org-e-texinfo-filter-section-blank-lines (headline back-end info)
409 "Filter controlling number of blank lines after a section."
410 (let ((blanks (make-string 2 ?\n)))
411 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline)))
413 (defun org-e-texinfo--find-copying (info)
414 "Retrieve the headline identified by the property :copying:.
416 INFO is the plist containing the export options and tree. It is
417 used to find and extract the single desired headline. This
418 cannot be treated as a standard headline since it must be
419 inserted in a specific location."
420 (let (copying)
421 (org-element-map (plist-get info :parse-tree) 'headline
422 (lambda (copy)
423 (when (org-element-property :copying copy)
424 (push copy copying))) info 't)
425 ;; Retrieve the single entry
426 (car copying)))
428 (defun org-e-texinfo--find-verb-separator (s)
429 "Return a character not used in string S.
430 This is used to choose a separator for constructs like \\verb."
431 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
432 (loop for c across ll
433 when (not (string-match (regexp-quote (char-to-string c)) s))
434 return (char-to-string c))))
436 (defun org-e-texinfo--make-option-string (options)
437 "Return a comma separated string of keywords and values.
438 OPTIONS is an alist where the key is the options keyword as
439 a string, and the value a list containing the keyword value, or
440 nil."
441 (mapconcat (lambda (pair)
442 (concat (first pair)
443 (when (> (length (second pair)) 0)
444 (concat "=" (second pair)))))
445 options
446 ","))
448 (defun org-e-texinfo--quotation-marks (text info)
449 "Export quotation marks using ` and ' as the markers.
450 TEXT is a string containing quotation marks to be replaced. INFO
451 is a plist used as a communication channel."
452 (mapc (lambda(l)
453 (let ((start 0))
454 (while (setq start (string-match (car l) text start))
455 (let ((new-quote (concat (match-string 1 text) (cdr l))))
456 (setq text (replace-match new-quote t t text))))))
457 (cdr org-e-texinfo-quotes))
458 text)
460 (defun org-e-texinfo--text-markup (text markup)
461 "Format TEXT depending on MARKUP text markup.
462 See `org-e-texinfo-text-markup-alist' for details."
463 (let ((fmt (cdr (assq markup org-e-texinfo-text-markup-alist))))
464 (cond
465 ;; No format string: Return raw text.
466 ((not fmt) text)
467 ((eq 'verb fmt)
468 (let ((separator (org-e-texinfo--find-verb-separator text)))
469 (concat "@verb{" separator text separator "}")))
470 ((eq 'code fmt)
471 (let ((start 0)
472 (rtn "")
473 char)
474 (while (string-match "[@{}]" text)
475 (setq char (match-string 0 text))
476 (if (> (match-beginning 0) 0)
477 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
478 (setq text (substring text (1+ (match-beginning 0))))
479 (setq char (concat "@" char)
480 rtn (concat rtn char)))
481 (setq text (concat rtn text)
482 fmt "@code{%s}")
483 (format fmt text)))
484 ;; Else use format string.
485 (t (format fmt text)))))
487 ;;; Headline sanitizing
489 (defun org-e-texinfo--sanitize-headline (headline info)
490 "Remove all formatting from the text of a headline for use in
491 node and menu listing."
492 (mapconcat 'identity
493 (org-e-texinfo--sanitize-headline-contents headline info) " "))
495 (defun org-e-texinfo--sanitize-headline-contents (headline info)
496 "Retrieve the content of the headline.
498 Any content that can contain further formatting is checked
499 recursively, to ensure that nested content is also properly
500 retrieved."
501 (loop for contents in headline append
502 (cond
503 ;; already a string
504 ((stringp contents)
505 (list (replace-regexp-in-string " $" "" contents)))
506 ;; Is exported as-is (value)
507 ((org-element-map contents '(verbatim code)
508 (lambda (value)
509 (org-element-property :value value))))
510 ;; Has content and recurse into the content
511 ((org-element-contents contents)
512 (org-e-texinfo--sanitize-headline-contents
513 (org-element-contents contents) info)))))
515 ;;; Menu sanitizing
517 (defun org-e-texinfo--sanitize-menu (title)
518 "Remove invalid characters from TITLE for use in menus and
519 nodes.
521 Based on TEXINFO specifications, the following must be removed:
522 @ { } ( ) : . ,"
523 (replace-regexp-in-string "[@{}():,.]" "" title))
525 ;;; Content sanitizing
527 (defun org-e-texinfo--sanitize-content (text)
528 "Ensure characters are properly escaped when used in headlines or blocks.
530 Escape characters are: @ { }"
531 (replace-regexp-in-string "\\\([@{}]\\\)" "@\\1" text))
533 ;;; Menu creation
535 (defun org-e-texinfo--build-menu (tree level info &optional detailed)
536 "Create the @menu/@end menu information from TREE at headline
537 level LEVEL.
539 TREE contains the parse-tree to work with, either of the entire
540 document or of a specific parent headline. LEVEL indicates what
541 level of headlines to look at when generating the menu. INFO is
542 a plist containing contextual information.
544 Detailed determines whether to build a single level of menu, or
545 recurse into all children as well."
546 (let ((menu (org-e-texinfo--generate-menu-list tree level info))
547 output text-menu)
548 (cond
549 (detailed
550 ;; Looping is done within the menu generation.
551 (setq text-menu (org-e-texinfo--generate-detailed menu level info)))
553 (setq text-menu (org-e-texinfo--generate-menu-items menu info))))
554 (when text-menu
555 (setq output (org-e-texinfo--format-menu text-menu))
556 (mapconcat 'identity output "\n"))))
558 (defun org-e-texinfo--generate-detailed (menu level info)
559 "Generate a detailed listing of all subheadings within MENU starting at LEVEL.
561 MENU is the parse-tree to work with. LEVEL is the starting level
562 for the menu headlines and from which recursion occurs. INFO is
563 a plist containing contextual information."
564 (when level
565 (let ((max-depth (plist-get info :headline-levels)))
566 (when (> max-depth level)
567 (loop for headline in menu append
568 (let* ((title (org-e-texinfo--menu-headlines headline info))
569 ;; Create list of menu entries for the next level
570 (sublist (org-e-texinfo--generate-menu-list
571 headline (1+ level) info))
572 ;; Generate the menu items for that level. If
573 ;; there are none omit that heading completely,
574 ;; otherwise join the title to it's related entries.
575 (submenu (if (org-e-texinfo--generate-menu-items sublist info)
576 (append (list title)
577 (org-e-texinfo--generate-menu-items sublist info))
578 'nil))
579 ;; Start the process over the next level down.
580 (recursion (org-e-texinfo--generate-detailed sublist (1+ level) info)))
581 (setq recursion (append submenu recursion))
582 recursion))))))
584 (defun org-e-texinfo--generate-menu-list (tree level info)
585 "Generate the list of headlines that are within a given level
586 of the tree for further formatting.
588 TREE is the parse-tree containing the headlines. LEVEL is the
589 headline level to generate a list of. INFO is a plist holding
590 contextual information."
591 (let (seq)
592 (org-element-map
593 tree 'headline
594 (lambda (head)
595 (when (org-element-property :level head)
596 (if (and (eq level (org-element-property :level head))
597 ;; Do not take note of footnotes or copying headlines
598 (not (org-element-property :copying head))
599 (not (org-element-property :footnote-section-p head)))
600 (push head seq)))))
601 ;; Return the list of headlines (reverse to have in actual order)
602 (reverse seq)))
604 (defun org-e-texinfo--generate-menu-items (items info)
605 "Generate a list of headline information from the listing ITEMS.
607 ITEMS is a list of the headlines to be converted into entries.
608 INFO is a plist containing contextual information.
610 Returns a list containing the following information from each
611 headline: length, title, description. This is used to format the
612 menu using `org-e-texinfo--format-menu'."
613 (loop for headline in items collect
614 (let* ((title (org-e-texinfo--sanitize-menu
615 (org-e-texinfo--sanitize-headline
616 (org-element-property :title headline) info)))
617 (descr (org-export-data
618 (org-element-property :description headline) info))
619 (len (length title))
620 (output (list len title descr)))
621 output)))
623 (defun org-e-texinfo--menu-headlines (headline info)
624 "Retrieve the title from HEADLINE.
626 INFO is a plist holding contextual information.
628 Return the headline as a list of (length title description) with
629 length of -1 and nil description. This is used in
630 `org-e-texinfo--format-menu' to identify headlines as opposed to
631 entries."
632 (let ((title (org-export-data
633 (org-element-property :title headline) info)))
634 (list -1 title 'nil)))
636 (defun org-e-texinfo--format-menu (text-menu)
637 "Format the TEXT-MENU items to be properly printed in the menu.
639 Each entry in the menu should be provided as (length title
640 description).
642 Headlines in the detailed menu are given length -1 to ensure they
643 are never confused with other entries. They also have no
644 description.
646 Other menu items are output as:
647 Title:: description
649 With the spacing between :: and description based on the length
650 of the longest menu entry."
652 (let* ((lengths (mapcar 'car text-menu))
653 (max-length (apply 'max lengths))
654 output)
655 (setq output
656 (mapcar (lambda (name)
657 (let* ((title (nth 1 name))
658 (desc (nth 2 name))
659 (length (nth 0 name)))
660 (if (> length -1)
661 (concat "* " title ":: "
662 (make-string
663 (- (+ 3 max-length) length)
664 ?\s)
665 (if desc
666 (concat desc)))
667 (concat "\n" title "\n"))))
668 text-menu))
669 output))
671 ;;; Template
673 (defun org-e-texinfo-template (contents info)
674 "Return complete document string after Texinfo conversion.
675 CONTENTS is the transcoded contents string. INFO is a plist
676 holding export options."
677 (let* ((title (org-export-data (plist-get info :title) info))
678 (info-filename (or (plist-get info :texinfo-filename)
679 (file-name-nondirectory
680 (org-export-output-file-name ".info"))))
681 (author (org-export-data (plist-get info :author) info))
682 (texinfo-header (plist-get info :texinfo-header))
683 (subtitle (plist-get info :subtitle))
684 (subauthor (plist-get info :subauthor))
685 (class (plist-get info :texinfo-class))
686 (header (nth 1 (assoc class org-e-texinfo-classes)))
687 (copying (org-e-texinfo--find-copying info))
688 (dircat (plist-get info :texinfo-dircat))
689 (dirtitle (plist-get info :texinfo-dirtitle))
690 (dirdesc (plist-get info :texinfo-dirdesc))
691 ;; Spacing to align description (column 32 - 3 for `* ' and
692 ;; `.' in text.
693 (dirspacing (- 29 (length dirtitle)))
694 (menu (org-e-texinfo-make-menu info 'main))
695 (detail-menu (org-e-texinfo-make-menu info 'detailed)))
696 (concat
697 ;; Header
698 header "\n"
699 "@c %**start of header\n"
700 ;; Filename and Title
701 "@setfilename " info-filename "\n"
702 "@settitle " title "\n"
703 "\n\n"
704 "@c Version and Contact Info\n"
705 "@set AUTHOR " author "\n"
707 ;; Additional Header Options set by `#+TEXINFO_HEADER
708 (if texinfo-header
709 (concat "\n"
710 texinfo-header
711 "\n"))
713 "@c %**end of header\n"
714 "@finalout\n"
715 "\n\n"
717 ;; Copying
718 "@copying\n"
719 ;; Only export the content of the headline, do not need the
720 ;; initial headline.
721 (org-export-data (nth 2 copying) info)
722 "@end copying\n"
723 "\n\n"
725 ;; Info directory information
726 ;; Only supply if both title and category are provided
727 (if (and dircat dirtitle)
728 (concat "@dircategory " dircat "\n"
729 "@direntry\n"
730 "* " dirtitle "."
731 (make-string dirspacing ?\s)
732 dirdesc "\n"
733 "@end direntry\n"))
734 "\n\n"
736 ;; Title
737 "@titlepage\n"
738 "@title " title "\n\n"
739 (if subtitle
740 (concat "@subtitle " subtitle "\n"))
741 "@author " author "\n"
742 (if subauthor
743 (concat subauthor "\n"))
744 "\n"
745 "@c The following two commands start the copyright page.\n"
746 "@page\n"
747 "@vskip 0pt plus 1filll\n"
748 "@insertcopying\n"
749 "@end titlepage\n\n"
750 "@c Output the table of contents at the beginning.\n"
751 "@contents\n\n"
753 ;; Configure Top Node when not for Tex
754 "@ifnottex\n"
755 "@node Top\n"
756 "@top " title " Manual\n"
757 "@insertcopying\n"
758 "@end ifnottex\n\n"
760 ;; Do not output menus if they are empty
761 (if menu
762 ;; Menu
763 (concat "@menu\n"
764 menu
765 "\n\n"
766 ;; Detailed Menu
767 (if detail-menu
768 (concat "@detailmenu\n"
769 " --- The Detailed Node Listing ---\n"
770 detail-menu
771 "\n\n"
772 "@end detailmenu\n"))
773 "@end menu\n"))
774 "\n\n"
776 ;; Document's body.
777 contents
778 "\n"
779 ;; Creator.
780 (let ((creator-info (plist-get info :with-creator)))
781 (cond
782 ((not creator-info) "")
783 ((eq creator-info 'comment)
784 (format "@c %s\n" (plist-get info :creator)))
785 (t (concat (plist-get info :creator) "\n"))))
786 ;; Document end.
787 "\n@bye")))
791 ;;; Transcode Functions
793 ;;; Babel Call
795 ;; Babel Calls are ignored.
797 ;;; Bold
799 (defun org-e-texinfo-bold (bold contents info)
800 "Transcode BOLD from Org to Texinfo.
801 CONTENTS is the text with bold markup. INFO is a plist holding
802 contextual information."
803 (org-e-texinfo--text-markup contents 'bold))
805 ;;; Center Block
807 ;; Center blocks are ignored
809 ;;; Clock
811 (defun org-e-texinfo-clock (clock contents info)
812 "Transcode a CLOCK element from Org to Texinfo.
813 CONTENTS is nil. INFO is a plist holding contextual
814 information."
815 (concat
816 "@noindent"
817 (format "@strong{%s} " org-clock-string)
818 (format org-e-texinfo-inactive-timestamp-format
819 (concat (org-translate-time (org-element-property :value clock))
820 (let ((time (org-element-property :time clock)))
821 (and time (format " (%s)" time)))))
822 "@*"))
824 ;;; Code
826 (defun org-e-texinfo-code (code contents info)
827 "Transcode a CODE object from Org to Texinfo.
828 CONTENTS is nil. INFO is a plist used as a communication
829 channel."
830 (org-e-texinfo--text-markup (org-element-property :value code) 'code))
832 ;;; Comment
834 (defun org-e-texinfo-comment (comment contents info)
835 "Transcode a COMMENT object from Org to Texinfo.
836 CONTENTS is the text in the comment. INFO is a plist holding
837 contextual information."
838 (org-e-texinfo--text-markup (org-element-property :value comment) 'comment))
840 ;;; Comment Block
842 (defun org-e-texinfo-comment-block (comment-block contents info)
843 "Transcode a COMMENT-BLOCK object from Org to Texinfo.
844 CONTENTS is the text within the block. INFO is a plist holding
845 contextual information."
846 (format "@ignore\n%s@end ignore" (org-element-property :value comment-block)))
848 ;;; Drawer
850 (defun org-e-texinfo-drawer (drawer contents info)
851 "Transcode a DRAWER element from Org to Texinfo.
852 CONTENTS holds the contents of the block. INFO is a plist
853 holding contextual information."
854 (let* ((name (org-element-property :drawer-name drawer))
855 (output (if (functionp org-e-texinfo-format-drawer-function)
856 (funcall org-e-texinfo-format-drawer-function
857 name contents)
858 ;; If there's no user defined function: simply
859 ;; display contents of the drawer.
860 contents)))
861 output))
863 ;;; Dynamic Block
865 (defun org-e-texinfo-dynamic-block (dynamic-block contents info)
866 "Transcode a DYNAMIC-BLOCK element from Org to Texinfo.
867 CONTENTS holds the contents of the block. INFO is a plist
868 holding contextual information. See `org-export-data'."
869 contents)
871 ;;; Entity
873 (defun org-e-texinfo-entity (entity contents info)
874 "Transcode an ENTITY object from Org to Texinfo.
875 CONTENTS are the definition itself. INFO is a plist holding
876 contextual information."
877 (let ((ent (org-element-property :latex entity)))
878 (if (org-element-property :latex-math-p entity) (format "@math{%s}" ent) ent)))
880 ;;; Example Block
882 (defun org-e-texinfo-example-block (example-block contents info)
883 "Transcode an EXAMPLE-BLOCK element from Org to Texinfo.
884 CONTENTS is nil. INFO is a plist holding contextual
885 information."
886 (format "@verbatim\n%s@end verbatim"
887 (org-export-format-code-default example-block info)))
889 ;;; Export Block
891 (defun org-e-texinfo-export-block (export-block contents info)
892 "Transcode a EXPORT-BLOCK element from Org to Texinfo.
893 CONTENTS is nil. INFO is a plist holding contextual information."
894 (when (string= (org-element-property :type export-block) "TEXINFO")
895 (org-remove-indentation (org-element-property :value export-block))))
897 ;;; Export Snippet
899 (defun org-e-texinfo-export-snippet (export-snippet contents info)
900 "Transcode a EXPORT-SNIPPET object from Org to Texinfo.
901 CONTENTS is nil. INFO is a plist holding contextual information."
902 (when (eq (org-export-snippet-backend export-snippet) 'e-texinfo)
903 (org-element-property :value export-snippet)))
905 ;;; Fixed Width
907 (defun org-e-texinfo-fixed-width (fixed-width contents info)
908 "Transcode a FIXED-WIDTH element from Org to Texinfo.
909 CONTENTS is nil. INFO is a plist holding contextual information."
910 (format "@example\n%s\n@end example"
911 (org-remove-indentation
912 (org-e-texinfo--sanitize-content
913 (org-element-property :value fixed-width)))))
915 ;;; Footnote Definition
917 ;; Footnote Definitions are ignored.
919 ;;; Footnote Reference
922 (defun org-e-texinfo-footnote-reference (footnote contents info)
923 "Create a footnote reference for FOOTNOTE.
925 FOOTNOTE is the footnote to define. CONTENTS is nil. INFO is a
926 plist holding contextual information."
927 (let ((def (org-export-get-footnote-definition footnote info)))
928 (format "@footnote{%s}"
929 (org-trim (org-export-data def info)))))
931 ;;; Headline
933 (defun org-e-texinfo-headline (headline contents info)
934 "Transcode an HEADLINE element from Org to Texinfo.
935 CONTENTS holds the contents of the headline. INFO is a plist
936 holding contextual information."
937 (let* ((class (plist-get info :texinfo-class))
938 (level (org-export-get-relative-level headline info))
939 (numberedp (org-export-numbered-headline-p headline info))
940 (class-sectionning (assoc class org-e-texinfo-classes))
941 ;; Find the index type, if any
942 (index (org-element-property :index headline))
943 ;; Retrieve headline text
944 (text (org-e-texinfo--sanitize-headline
945 (org-element-property :title headline) info))
946 ;; Create node info, to insert it before section formatting.
947 (node (format "@node %s\n"
948 (org-e-texinfo--sanitize-menu
949 (replace-regexp-in-string "%" "%%" text))))
950 ;; Menus must be generated with first child, otherwise they
951 ;; will not nest properly
952 (menu (let* ((first (org-export-first-sibling-p headline info))
953 (parent (org-export-get-parent-headline headline))
954 (title (org-e-texinfo--sanitize-headline
955 (org-element-property :title parent) info))
956 heading listing
957 (tree (plist-get info :parse-tree)))
958 (if first
959 (org-element-map
960 (plist-get info :parse-tree) 'headline
961 (lambda (ref)
962 (if (member title (org-element-property :title ref))
963 (push ref heading)))
964 info 't))
965 (setq listing (org-e-texinfo--build-menu
966 (car heading) level info))
967 (if listing
968 (setq listing (replace-regexp-in-string
969 "%" "%%" listing)
970 listing (format
971 "\n@menu\n%s\n@end menu\n\n" listing))
972 'nil)))
973 ;; Section formatting will set two placeholders: one for the
974 ;; title and the other for the contents.
975 (section-fmt
976 (let ((sec (if (and (symbolp (nth 2 class-sectionning))
977 (fboundp (nth 2 class-sectionning)))
978 (funcall (nth 2 class-sectionning) level numberedp)
979 (nth (1+ level) class-sectionning))))
980 (cond
981 ;; No section available for that LEVEL.
982 ((not sec) nil)
983 ;; Section format directly returned by a function.
984 ((stringp sec) sec)
985 ;; (numbered-section . unnumbered-section)
986 ((not (consp (cdr sec)))
987 ;; If an index, always unnumbered
988 (if index
989 (concat menu node (cdr sec) "\n%s")
990 ;; Otherwise number as needed.
991 (concat menu node
992 (funcall
993 (if numberedp #'car #'cdr) sec) "\n%s"))))))
994 (todo
995 (and (plist-get info :with-todo-keywords)
996 (let ((todo (org-element-property :todo-keyword headline)))
997 (and todo (org-export-data todo info)))))
998 (todo-type (and todo (org-element-property :todo-type headline)))
999 (tags (and (plist-get info :with-tags)
1000 (org-export-get-tags headline info)))
1001 (priority (and (plist-get info :with-priority)
1002 (org-element-property :priority headline)))
1003 ;; Create the headline text along with a no-tag version. The
1004 ;; latter is required to remove tags from table of contents.
1005 (full-text (org-e-texinfo--sanitize-content
1006 (if (functionp org-e-texinfo-format-headline-function)
1007 ;; User-defined formatting function.
1008 (funcall org-e-texinfo-format-headline-function
1009 todo todo-type priority text tags)
1010 ;; Default formatting.
1011 (concat
1012 (when todo
1013 (format "@strong{%s} " todo))
1014 (when priority (format "@emph{#%s} " priority))
1015 text
1016 (when tags
1017 (format ":%s:"
1018 (mapconcat 'identity tags ":")))))))
1019 (full-text-no-tag
1020 (org-e-texinfo--sanitize-content
1021 (if (functionp org-e-texinfo-format-headline-function)
1022 ;; User-defined formatting function.
1023 (funcall org-e-texinfo-format-headline-function
1024 todo todo-type priority text nil)
1025 ;; Default formatting.
1026 (concat
1027 (when todo (format "@strong{%s} " todo))
1028 (when priority (format "@emph{#%c} " priority))
1029 text))))
1030 (pre-blanks
1031 (make-string (org-element-property :pre-blank headline) 10)))
1032 (cond
1033 ;; Case 1: This is a footnote section: ignore it.
1034 ((org-element-property :footnote-section-p headline) nil)
1035 ;; Case 2: This is the `copying' section: ignore it
1036 ;; This is used elsewhere.
1037 ((org-element-property :copying headline) nil)
1038 ;; Case 3: An index. If it matches one of the known indexes,
1039 ;; print it as such following the contents, otherwise
1040 ;; print the contents and leave the index up to the user.
1041 (index
1042 (format
1043 section-fmt full-text
1044 (concat pre-blanks contents "\n"
1045 (if (member index '("cp" "fn" "ky" "pg" "tp" "vr"))
1046 (concat "@printindex " index)))))
1047 ;; Case 4: This is a deep sub-tree: export it as a list item.
1048 ;; Also export as items headlines for which no section
1049 ;; format has been found.
1050 ((or (not section-fmt) (org-export-low-level-p headline info))
1051 ;; Build the real contents of the sub-tree.
1052 (let ((low-level-body
1053 (concat
1054 ;; If the headline is the first sibling, start a list.
1055 (when (org-export-first-sibling-p headline info)
1056 (format "@%s\n" (if numberedp 'enumerate 'itemize)))
1057 ;; Itemize headline
1058 "@item\n" full-text "\n" pre-blanks contents)))
1059 ;; If headline is not the last sibling simply return
1060 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1061 ;; blank line.
1062 (if (not (org-export-last-sibling-p headline info)) low-level-body
1063 (replace-regexp-in-string
1064 "[ \t\n]*\\'"
1065 (format "\n@end %s" (if numberedp 'enumerate 'itemize))
1066 low-level-body))))
1067 ;; Case 5: Standard headline. Export it as a section.
1069 (cond
1070 ((not (and tags (eq (plist-get info :with-tags) 'not-in-toc)))
1071 ;; Regular section. Use specified format string.
1072 (format (replace-regexp-in-string "%]" "%%]" section-fmt) full-text
1073 (concat pre-blanks contents)))
1074 ((string-match "\\`@\\(.*?\\){" section-fmt)
1075 ;; If tags should be removed from table of contents, insert
1076 ;; title without tags as an alternative heading in sectioning
1077 ;; command.
1078 (format (replace-match (concat (match-string 1 section-fmt) "[%s]")
1079 nil nil section-fmt 1)
1080 ;; Replace square brackets with parenthesis since
1081 ;; square brackets are not supported in optional
1082 ;; arguments.
1083 (replace-regexp-in-string
1084 "\\[" "("
1085 (replace-regexp-in-string
1086 "\\]" ")"
1087 full-text-no-tag))
1088 full-text
1089 (concat pre-blanks contents)))
1091 ;; Impossible to add an alternative heading. Fallback to
1092 ;; regular sectioning format string.
1093 (format (replace-regexp-in-string "%]" "%%]" section-fmt) full-text
1094 (concat pre-blanks contents))))))))
1096 ;;; Horizontal Rule
1098 ;; Horizontal rules are ignored
1100 ;;; Inline Babel Call
1102 ;; Inline Babel Calls are ignored.
1104 ;;; Inline Src Block
1106 (defun org-e-texinfo-inline-src-block (inline-src-block contents info)
1107 "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
1108 CONTENTS holds the contents of the item. INFO is a plist holding
1109 contextual information."
1110 (let* ((code (org-element-property :value inline-src-block))
1111 (separator (org-e-texinfo--find-verb-separator code)))
1112 (concat "@verb{" separator code separator "}")))
1114 ;;; Inlinetask
1116 (defun org-e-texinfo-inlinetask (inlinetask contents info)
1117 "Transcode an INLINETASK element from Org to Texinfo.
1118 CONTENTS holds the contents of the block. INFO is a plist
1119 holding contextual information."
1120 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1121 (todo (and (plist-get info :with-todo-keywords)
1122 (let ((todo (org-element-property :todo-keyword inlinetask)))
1123 (and todo (org-export-data todo info)))))
1124 (todo-type (org-element-property :todo-type inlinetask))
1125 (tags (and (plist-get info :with-tags)
1126 (org-export-get-tags inlinetask info)))
1127 (priority (and (plist-get info :with-priority)
1128 (org-element-property :priority inlinetask))))
1129 ;; If `org-e-texinfo-format-inlinetask-function' is provided, call it
1130 ;; with appropriate arguments.
1131 (if (functionp org-e-texinfo-format-inlinetask-function)
1132 (funcall org-e-texinfo-format-inlinetask-function
1133 todo todo-type priority title tags contents)
1134 ;; Otherwise, use a default template.
1135 (let ((full-title
1136 (concat
1137 (when todo (format "@strong{%s} " todo))
1138 (when priority (format "#%c " priority))
1139 title
1140 (when tags (format ":%s:"
1141 (mapconcat 'identity tags ":"))))))
1142 (format (concat "@center %s\n\n"
1143 "%s"
1144 "\n")
1145 full-title contents)))))
1147 ;;; Italic
1149 (defun org-e-texinfo-italic (italic contents info)
1150 "Transcode ITALIC from Org to Texinfo.
1151 CONTENTS is the text with italic markup. INFO is a plist holding
1152 contextual information."
1153 (org-e-texinfo--text-markup contents 'italic))
1155 ;;; Item
1157 (defun org-e-texinfo-item (item contents info)
1158 "Transcode an ITEM element from Org to Texinfo.
1159 CONTENTS holds the contents of the item. INFO is a plist holding
1160 contextual information."
1161 (let* ((tag (org-element-property :tag item))
1162 (desc (org-export-data tag info)))
1163 (concat "\n@item " (if tag desc) "\n"
1164 (org-trim contents) "\n")))
1166 ;;; Keyword
1168 (defun org-e-texinfo-keyword (keyword contents info)
1169 "Transcode a KEYWORD element from Org to Texinfo.
1170 CONTENTS is nil. INFO is a plist holding contextual information."
1171 (let ((key (org-element-property :key keyword))
1172 (value (org-element-property :value keyword)))
1173 (cond
1174 ((string= key "TEXINFO") value)
1175 ((string= key "CINDEX") (format "@cindex %s" value))
1176 ((string= key "FINDEX") (format "@findex %s" value))
1177 ((string= key "KINDEX") (format "@kindex %s" value))
1178 ((string= key "PINDEX") (format "@pindex %s" value))
1179 ((string= key "TINDEX") (format "@tindex %s" value))
1180 ((string= key "VINDEX") (format "@vindex %s" value)))))
1182 ;;; Latex Environment
1184 ;; Latex environments are ignored
1186 ;;; Latex Fragment
1188 ;; Latex fragments are ignored.
1190 ;;; Line Break
1192 (defun org-e-texinfo-line-break (line-break contents info)
1193 "Transcode a LINE-BREAK object from Org to Texinfo.
1194 CONTENTS is nil. INFO is a plist holding contextual information."
1195 "@*")
1197 ;;; Link
1199 (defun org-e-texinfo-link (link desc info)
1200 "Transcode a LINK object from Org to Texinfo.
1202 DESC is the description part of the link, or the empty string.
1203 INFO is a plist holding contextual information. See
1204 `org-export-data'."
1205 (let* ((type (org-element-property :type link))
1206 (raw-path (org-element-property :path link))
1207 ;; Ensure DESC really exists, or set it to nil.
1208 (desc (and (not (string= desc "")) desc))
1209 (path (cond
1210 ((member type '("http" "https" "ftp"))
1211 (concat type ":" raw-path))
1212 ((string= type "file")
1213 (if (file-name-absolute-p raw-path)
1214 (concat "file://" (expand-file-name raw-path))
1215 (concat "file://" raw-path)))
1216 (t raw-path)))
1217 (email (if (string= type "mailto")
1218 (let ((text (replace-regexp-in-string
1219 "@" "@@" raw-path)))
1220 (concat text (if desc (concat "," desc))))))
1221 protocol)
1222 (cond
1223 ;; Links pointing to an headline: Find destination and build
1224 ;; appropriate referencing command.
1225 ((member type '("custom-id" "id"))
1226 (let ((destination (org-export-resolve-id-link link info)))
1227 (case (org-element-type destination)
1228 ;; Id link points to an external file.
1229 (plain-text
1230 (if desc (format "@uref{file://%s,%s}" destination desc)
1231 (format "@uref{file://%s}" destination)))
1232 ;; LINK points to an headline. Use the headline as the NODE target
1233 (headline
1234 (format "@ref{%s}"
1235 (org-export-data
1236 (org-element-property :title destination) info)))
1237 (otherwise
1238 (let ((path (org-export-solidify-link-text path)))
1239 (if (not desc) (format "@ref{%s}" path)
1240 (format "@ref{%s,,%s}" path desc)))))))
1241 ((member type '("fuzzy"))
1242 (let ((destination (org-export-resolve-fuzzy-link link info)))
1243 (case (org-element-type destination)
1244 ;; Id link points to an external file.
1245 (plain-text
1246 (if desc (format "@uref{file://%s,%s}" destination desc)
1247 (format "@uref{file://%s}" destination)))
1248 ;; LINK points to an headline. Use the headline as the NODE target
1249 (headline
1250 (format "@ref{%s}"
1251 (org-export-data
1252 (org-element-property :title destination) info)))
1253 (otherwise
1254 (let ((path (org-export-solidify-link-text path)))
1255 (if (not desc) (format "@ref{%s}" path)
1256 (format "@ref{%s,,%s}" path desc)))))))
1257 ;; Special case for email addresses
1258 (email
1259 (format "@email{%s}" email))
1260 ;; External link with a description part.
1261 ((and path desc) (format "@uref{%s,%s}" path desc))
1262 ;; External link without a description part.
1263 (path (format "@uref{%s}" path))
1264 ;; No path, only description. Try to do something useful.
1265 (t (format org-e-texinfo-link-with-unknown-path-format desc)))))
1268 ;;; Menu
1270 (defun org-e-texinfo-make-menu (info level)
1271 "Create the menu for inclusion in the texifo document.
1273 INFO is the parsed buffer that contains the headlines. LEVEL
1274 determines whether to make the main menu, or the detailed menu.
1276 This is only used for generating the primary menu. In-Node menus
1277 are generated directly."
1278 (let* ((parse (plist-get info :parse-tree))
1279 ;; Top determines level to build menu from, it finds the
1280 ;; level of the first headline in the export.
1281 (top (org-element-map
1282 parse 'headline
1283 (lambda (headline)
1284 (org-element-property :level headline)) info 't)))
1285 (cond
1286 ;; Generate the main menu
1287 ((eq level 'main)
1288 (org-e-texinfo--build-menu parse top info))
1289 ;; Generate the detailed (recursive) menu
1290 ((eq level 'detailed)
1291 ;; Requires recursion
1292 ;;(org-e-texinfo--build-detailed-menu parse top info)
1293 (org-e-texinfo--build-menu parse top info 'detailed))
1294 ;; Otherwise do nothing
1295 (t))))
1297 ;;; Paragraph
1299 (defun org-e-texinfo-paragraph (paragraph contents info)
1300 "Transcode a PARAGRAPH element from Org to Texinfo.
1301 CONTENTS is the contents of the paragraph, as a string. INFO is
1302 the plist used as a communication channel."
1303 contents)
1305 ;;; Plain List
1307 (defun org-e-texinfo-plain-list (plain-list contents info)
1308 "Transcode a PLAIN-LIST element from Org to Texinfo.
1309 CONTENTS is the contents of the list. INFO is a plist holding
1310 contextual information."
1311 (let* ((attr (org-export-read-attribute :attr_texinfo plain-list))
1312 (indic (or (plist-get attr :indic)
1313 org-e-texinfo-def-table-markup))
1314 (type (org-element-property :type plain-list))
1315 (table-type (or (plist-get attr :table-type)
1316 "table"))
1317 ;; Ensure valid texinfo table type.
1318 (table-type (if (memq table-type '("table" "ftable" "vtable"))
1319 table-type
1320 "table"))
1321 (list-type (cond
1322 ((eq type 'ordered) "enumerate")
1323 ((eq type 'unordered) "itemize")
1324 ((eq type 'descriptive) table-type))))
1325 (format "@%s%s\n@end %s"
1326 (if (eq type 'descriptive)
1327 (concat list-type " " indic)
1328 list-type)
1329 contents
1330 list-type)))
1332 ;;; Plain Text
1334 (defun org-e-texinfo-plain-text (text info)
1335 "Transcode a TEXT string from Org to Texinfo.
1336 TEXT is the string to transcode. INFO is a plist holding
1337 contextual information."
1338 ;; LaTeX into @LaTeX{} and TeX into @TeX{}
1339 (let ((case-fold-search nil)
1340 (start 0))
1341 (while (string-match "\\(\\(?:La\\)?TeX\\)" text start)
1342 (setq text (replace-match
1343 (format "@%s{}" (match-string 1 text)) nil t text)
1344 start (match-end 0))))
1345 ;; Handle quotation marks
1346 (setq text (org-e-texinfo--quotation-marks text info))
1347 ;; Convert special strings.
1348 (when (plist-get info :with-special-strings)
1349 (while (string-match (regexp-quote "...") text)
1350 (setq text (replace-match "@dots{}" nil t text))))
1351 ;; Handle break preservation if required.
1352 (when (plist-get info :preserve-breaks)
1353 (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n"
1354 text)))
1355 ;; Return value with @ { and } protected.
1356 (org-e-texinfo--sanitize-content text))
1358 ;;; Planning
1360 (defun org-e-texinfo-planning (planning contents info)
1361 "Transcode a PLANNING element from Org to Texinfo.
1362 CONTENTS is nil. INFO is a plist holding contextual
1363 information."
1364 (concat
1365 "@noindent"
1366 (mapconcat
1367 'identity
1368 (delq nil
1369 (list
1370 (let ((closed (org-element-property :closed planning)))
1371 (when closed
1372 (concat
1373 (format "@strong%s} " org-closed-string)
1374 (format org-e-texinfo-inactive-timestamp-format
1375 (org-translate-time closed)))))
1376 (let ((deadline (org-element-property :deadline planning)))
1377 (when deadline
1378 (concat
1379 (format "@strong{%s} " org-deadline-string)
1380 (format org-e-texinfo-active-timestamp-format
1381 (org-translate-time deadline)))))
1382 (let ((scheduled (org-element-property :scheduled planning)))
1383 (when scheduled
1384 (concat
1385 (format "@strong{%s} " org-scheduled-string)
1386 (format org-e-texinfo-active-timestamp-format
1387 (org-translate-time scheduled)))))))
1388 " ")
1389 "@*"))
1391 ;;; Property Drawer
1393 (defun org-e-texinfo-property-drawer (property-drawer contents info)
1394 "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
1395 CONTENTS is nil. INFO is a plist holding contextual
1396 information."
1397 ;; The property drawer isn't exported but we want separating blank
1398 ;; lines nonetheless.
1401 ;;; Quote Block
1403 (defun org-e-texinfo-quote-block (quote-block contents info)
1404 "Transcode a QUOTE-BLOCK element from Org to Texinfo.
1405 CONTENTS holds the contents of the block. INFO is a plist
1406 holding contextual information."
1407 (let* ((title (org-element-property :name quote-block))
1408 (start-quote (concat "@quotation"
1409 (if title
1410 (format " %s" title)))))
1411 (format "%s\n%s@end quotation" start-quote contents)))
1413 ;;; Quote Section
1415 (defun org-e-texinfo-quote-section (quote-section contents info)
1416 "Transcode a QUOTE-SECTION element from Org to Texinfo.
1417 CONTENTS is nil. INFO is a plist holding contextual information."
1418 (let ((value (org-remove-indentation
1419 (org-element-property :value quote-section))))
1420 (when value (format "@verbatim\n%s@end verbatim" value))))
1422 ;;; Radio Target
1424 (defun org-e-texinfo-radio-target (radio-target text info)
1425 "Transcode a RADIO-TARGET object from Org to Texinfo.
1426 TEXT is the text of the target. INFO is a plist holding
1427 contextual information."
1428 (format "@anchor{%s}%s"
1429 (org-export-solidify-link-text
1430 (org-element-property :value radio-target))
1431 text))
1433 ;;; Section
1435 (defun org-e-texinfo-section (section contents info)
1436 "Transcode a SECTION element from Org to Texinfo.
1437 CONTENTS holds the contents of the section. INFO is a plist
1438 holding contextual information."
1439 contents)
1441 ;;; Special Block
1443 ;; Are ignored at the moment
1445 ;;; Src Block
1447 (defun org-e-texinfo-src-block (src-block contents info)
1448 "Transcode a SRC-BLOCK element from Org to Texinfo.
1449 CONTENTS holds the contents of the item. INFO is a plist holding
1450 contextual information."
1451 (let* ((lang (org-element-property :language src-block))
1452 (lisp-p (string-match-p "lisp" lang)))
1453 (cond
1454 ;; Case 1. Lisp Block
1455 (lisp-p
1456 (format "@lisp\n%s\n@end lisp"
1457 (org-export-format-code-default src-block info)))
1458 ;; Case 2. Other blocks
1460 (format "@example\n%s\n@end example"
1461 (org-export-format-code-default src-block info))))))
1463 ;;; Statistics Cookie
1465 (defun org-e-texinfo-statistics-cookie (statistics-cookie contents info)
1466 "Transcode a STATISTICS-COOKIE object from Org to Texinfo.
1467 CONTENTS is nil. INFO is a plist holding contextual information."
1468 (org-element-property :value statistics-cookie))
1470 ;;; Strike-Through
1472 ;; Strikethrough is ignored
1474 ;;; Subscript
1476 (defun org-e-texinfo-subscript (subscript contents info)
1477 "Transcode a SUBSCRIPT object from Org to Texinfo.
1478 CONTENTS is the contents of the object. INFO is a plist holding
1479 contextual information."
1480 (format "@math{_%s}" contents))
1482 ;;; Superscript
1484 (defun org-e-texinfo-superscript (superscript contents info)
1485 "Transcode a SUPERSCRIPT object from Org to Texinfo.
1486 CONTENTS is the contents of the object. INFO is a plist holding
1487 contextual information."
1488 (format "@math{^%s}" contents))
1490 ;;; Table
1492 ;; `org-e-texinfo-table' is the entry point for table transcoding. It
1493 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
1494 ;; delegates the job to either `org-e-texinfo-table--table.el-table' or
1495 ;; `org-e-texinfo-table--org-table' functions, depending of the type of
1496 ;; the table.
1498 ;; `org-e-texinfo-table--align-string' is a subroutine used to build
1499 ;; alignment string for Org tables.
1501 (defun org-e-texinfo-table (table contents info)
1502 "Transcode a TABLE element from Org to Texinfo.
1503 CONTENTS is the contents of the table. INFO is a plist holding
1504 contextual information."
1505 (cond
1506 ;; Case 1: verbatim table.
1507 ((or org-e-texinfo-tables-verbatim
1508 (let ((attr (mapconcat 'identity
1509 (org-element-property :attr_latex table)
1510 " ")))
1511 (and attr (string-match "\\<verbatim\\>" attr))))
1512 (format "@verbatim \n%s\n@end verbatim"
1513 ;; Re-create table, without affiliated keywords.
1514 (org-trim
1515 (org-element-interpret-data
1516 `(table nil ,@(org-element-contents table))))))
1517 ;; Case 2: table.el table. Convert it using appropriate tools.
1518 ((eq (org-element-property :type table) 'table.el)
1519 (org-e-texinfo-table--table.el-table table contents info))
1520 ;; Case 3: Standard table.
1521 (t (org-e-texinfo-table--org-table table contents info))))
1523 (defun org-e-texinfo-table-column-widths (table info)
1524 "Determine the largest table cell in each column to process alignment.
1526 TABLE is the table element to transcode. INFO is a plist used as
1527 a communication channel."
1528 (let* ((rows (org-element-map table 'table-row 'identity info))
1529 (collected (loop for row in rows collect
1530 (org-element-map
1531 row 'table-cell 'identity info)))
1532 (number-cells (length (car collected)))
1533 cells counts)
1534 (loop for row in collected do
1535 (push (mapcar (lambda (ref)
1536 (let* ((start (org-element-property :contents-begin ref))
1537 (end (org-element-property :contents-end ref))
1538 (length (- end start)))
1539 length)) row) cells))
1540 (setq cells (org-remove-if 'null cells))
1541 (push (loop for count from 0 to (- number-cells 1) collect
1542 (loop for item in cells collect
1543 (nth count item))) counts)
1544 (mapconcat (lambda (size)
1545 (make-string size ?a)) (mapcar (lambda (ref)
1546 (apply 'max `,@ref)) (car counts))
1547 "} {")))
1549 (defun org-e-texinfo-table--org-table (table contents info)
1550 "Return appropriate Texinfo code for an Org table.
1552 TABLE is the table type element to transcode. CONTENTS is its
1553 contents, as a string. INFO is a plist used as a communication
1554 channel.
1556 This function assumes TABLE has `org' as its `:type' attribute."
1557 (let* ((attr (org-export-read-attribute :attr_texinfo table))
1558 (col-width (plist-get attr :columns))
1559 (columns (if col-width
1560 (format "@columnfractions %s"
1561 col-width)
1562 (format "{%s}"
1563 (org-e-texinfo-table-column-widths
1564 table info)))))
1565 ;; Prepare the final format string for the table.
1566 (cond
1567 ;; Longtable.
1568 ;; Others.
1569 (t (concat
1570 (format "@multitable %s\n%s@end multitable"
1571 columns
1572 contents))))))
1574 (defun org-e-texinfo-table--table.el-table (table contents info)
1575 "Returns nothing.
1577 Rather than return an invalid table, nothing is returned."
1578 'nil)
1580 ;;; Table Cell
1582 (defun org-e-texinfo-table-cell (table-cell contents info)
1583 "Transcode a TABLE-CELL element from Org to Texinfo.
1584 CONTENTS is the cell contents. INFO is a plist used as
1585 a communication channel."
1586 (concat (if (and contents
1587 org-e-texinfo-table-scientific-notation
1588 (string-match orgtbl-exp-regexp contents))
1589 ;; Use appropriate format string for scientific
1590 ;; notation.
1591 (format org-e-texinfo-table-scientific-notation
1592 (match-string 1 contents)
1593 (match-string 2 contents))
1594 contents)
1595 (when (org-export-get-next-element table-cell info) "\n@tab ")))
1597 ;;; Table Row
1599 (defun org-e-texinfo-table-row (table-row contents info)
1600 "Transcode a TABLE-ROW element from Org to Texinfo.
1601 CONTENTS is the contents of the row. INFO is a plist used as
1602 a communication channel."
1603 ;; Rules are ignored since table separators are deduced from
1604 ;; borders of the current row.
1605 (when (eq (org-element-property :type table-row) 'standard)
1606 (let ((rowgroup-tag
1607 (cond
1608 ;; Case 1: Belongs to second or subsequent rowgroup.
1609 ((not (= 1 (org-export-table-row-group table-row info)))
1610 "@item ")
1611 ;; Case 2: Row is from first rowgroup. Table has >=1 rowgroups.
1612 ((org-export-table-has-header-p
1613 (org-export-get-parent-table table-row) info)
1614 "@headitem ")
1615 ;; Case 3: Row is from first and only row group.
1616 (t "@item "))))
1617 (when (eq (org-element-property :type table-row) 'standard)
1618 (concat rowgroup-tag contents "\n")))))
1620 ;;; Target
1622 (defun org-e-texinfo-target (target contents info)
1623 "Transcode a TARGET object from Org to Texinfo.
1624 CONTENTS is nil. INFO is a plist holding contextual
1625 information."
1626 (format "@anchor{%s}"
1627 (org-export-solidify-link-text (org-element-property :value target))))
1629 ;;; Timestamp
1631 (defun org-e-texinfo-timestamp (timestamp contents info)
1632 "Transcode a TIMESTAMP object from Org to Texinfo.
1633 CONTENTS is nil. INFO is a plist holding contextual
1634 information."
1635 (let ((value (org-translate-time (org-element-property :value timestamp)))
1636 (type (org-element-property :type timestamp)))
1637 (cond ((memq type '(active active-range))
1638 (format org-e-texinfo-active-timestamp-format value))
1639 ((memq type '(inactive inactive-range))
1640 (format org-e-texinfo-inactive-timestamp-format value))
1641 (t (format org-e-texinfo-diary-timestamp-format value)))))
1643 ;;; Underline
1645 ;; Underline is ignored
1647 ;;; Verbatim
1649 (defun org-e-texinfo-verbatim (verbatim contents info)
1650 "Transcode a VERBATIM object from Org to Texinfo.
1651 CONTENTS is nil. INFO is a plist used as a communication
1652 channel."
1653 (org-e-texinfo--text-markup (org-element-property :value verbatim) 'verbatim))
1655 ;;; Verse Block
1657 (defun org-e-texinfo-verse-block (verse-block contents info)
1658 "Transcode a VERSE-BLOCK element from Org to Texinfo.
1659 CONTENTS is verse block contents. INFO is a plist holding
1660 contextual information."
1661 ;; In a verse environment, add a line break to each newline
1662 ;; character and change each white space at beginning of a line
1663 ;; into a space of 1 em. Also change each blank line with
1664 ;; a vertical space of 1 em.
1665 (progn
1666 (setq contents (replace-regexp-in-string
1667 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
1668 (replace-regexp-in-string
1669 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
1670 (while (string-match "^[ \t]+" contents)
1671 (let ((new-str (format "\\hspace*{%dem}"
1672 (length (match-string 0 contents)))))
1673 (setq contents (replace-match new-str nil t contents))))
1674 (format "\\begin{verse}\n%s\\end{verse}" contents)))
1677 ;;; Interactive functions
1679 (defun org-e-texinfo-export-to-texinfo
1680 (&optional subtreep visible-only body-only ext-plist pub-dir)
1681 "Export current buffer to a Texinfo file.
1683 If narrowing is active in the current buffer, only export its
1684 narrowed part.
1686 If a region is active, export that region.
1688 When optional argument SUBTREEP is non-nil, export the sub-tree
1689 at point, extracting information from the headline properties
1690 first.
1692 When optional argument VISIBLE-ONLY is non-nil, don't export
1693 contents of hidden elements.
1695 When optional argument BODY-ONLY is non-nil, only write code
1696 between \"\\begin{document}\" and \"\\end{document}\".
1698 EXT-PLIST, when provided, is a property list with external
1699 parameters overriding Org default settings, but still inferior to
1700 file-local settings.
1702 When optional argument PUB-DIR is set, use it as the publishing
1703 directory.
1705 Return output file's name."
1706 (interactive)
1707 (let ((outfile (org-export-output-file-name ".texi" subtreep pub-dir)))
1708 (org-export-to-file
1709 'e-texinfo outfile subtreep visible-only body-only ext-plist)))
1711 (defun org-e-texinfo-export-to-info
1712 (&optional subtreep visible-only body-only ext-plist pub-dir)
1713 "Export current buffer to Texinfo then process through to INFO.
1715 If narrowing is active in the current buffer, only export its
1716 narrowed part.
1718 If a region is active, export that region.
1720 When optional argument SUBTREEP is non-nil, export the sub-tree
1721 at point, extracting information from the headline properties
1722 first.
1724 When optional argument VISIBLE-ONLY is non-nil, don't export
1725 contents of hidden elements.
1727 When optional argument BODY-ONLY is non-nil, only write code
1728 between \"\\begin{document}\" and \"\\end{document}\".
1730 EXT-PLIST, when provided, is a property list with external
1731 parameters overriding Org default settings, but still inferior to
1732 file-local settings.
1734 When optional argument PUB-DIR is set, use it as the publishing
1735 directory.
1737 Return INFO file's name."
1738 (interactive)
1739 (org-e-texinfo-compile
1740 (org-e-texinfo-export-to-texinfo
1741 subtreep visible-only body-only ext-plist pub-dir)))
1743 (defun org-e-texinfo-compile (texifile)
1744 "Compile a texinfo file.
1746 TEXIFILE is the name of the file being compiled. Processing is
1747 done through the command specified in `org-e-texinfo-info-process'.
1749 Return INFO file name or an error if it couldn't be produced."
1750 (let* ((wconfig (current-window-configuration))
1751 (texifile (file-truename texifile))
1752 (base (file-name-sans-extension texifile))
1753 errors)
1754 (message (format "Processing Texinfo file %s ..." texifile))
1755 (unwind-protect
1756 (progn
1757 (cond
1758 ;; A function is provided: Apply it.
1759 ((functionp org-e-texinfo-info-process)
1760 (funcall org-e-texinfo-info-process (shell-quote-argument texifile)))
1761 ;; A list is provided: Replace %b, %f and %o with appropriate
1762 ;; values in each command before applying it. Output is
1763 ;; redirected to "*Org INFO Texinfo Output*" buffer.
1764 ((consp org-e-texinfo-info-process)
1765 (let* ((out-dir (or (file-name-directory texifile) "./"))
1766 (outbuf (get-buffer-create "*Org Info Texinfo Output*")))
1767 (mapc
1768 (lambda (command)
1769 (shell-command
1770 (replace-regexp-in-string
1771 "%b" (shell-quote-argument base)
1772 (replace-regexp-in-string
1773 "%f" (shell-quote-argument texifile)
1774 (replace-regexp-in-string
1775 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1776 outbuf))
1777 org-e-texinfo-info-process)
1778 ;; Collect standard errors from output buffer.
1779 (setq errors (org-e-texinfo-collect-errors outbuf))))
1780 (t (error "No valid command to process to Info")))
1781 (let ((infofile (concat base ".info")))
1782 ;; Check for process failure. Provide collected errors if
1783 ;; possible.
1784 (if (not (file-exists-p infofile))
1785 (error (concat (format "INFO file %s wasn't produced" infofile)
1786 (when errors (concat ": " errors))))
1787 ;; Else remove log files, when specified, and signal end of
1788 ;; process to user, along with any error encountered.
1789 (message (concat "Process completed"
1790 (if (not errors) "."
1791 (concat " with errors: " errors)))))
1792 ;; Return output file name.
1793 infofile))
1794 (set-window-configuration wconfig))))
1796 (defun org-e-texinfo-collect-errors (buffer)
1797 "Collect some kind of errors from \"makeinfo\" command output.
1799 BUFFER is the buffer containing output.
1801 Return collected error types as a string, or nil if there was
1802 none."
1803 (with-current-buffer buffer
1804 (save-excursion
1805 (goto-char (point-min))
1806 ;; Find final "makeinfo" run.
1807 (when t
1808 (let ((case-fold-search t)
1809 (errors ""))
1810 (when (save-excursion
1811 (re-search-forward "perhaps incorrect sectioning?" nil t))
1812 (setq errors (concat errors " [incorrect sectionnng]")))
1813 (when (save-excursion
1814 (re-search-forward "missing close brace" nil t))
1815 (setq errors (concat errors " [syntax error]")))
1816 (when (save-excursion
1817 (re-search-forward "Unknown command" nil t))
1818 (setq errors (concat errors " [undefined @command]")))
1819 (when (save-excursion
1820 (re-search-forward "No matching @end" nil t))
1821 (setq errors (concat errors " [block incomplete]")))
1822 (when (save-excursion
1823 (re-search-forward "requires a sectioning" nil t))
1824 (setq errors (concat errors " [invalid section command]")))
1825 (when (save-excursion
1826 (re-search-forward "\\[unexpected\]" nil t))
1827 (setq errors (concat errors " [unexpected error]")))
1828 (when (save-excursion
1829 (re-search-forward "misplaced " nil t))
1830 (setq errors (concat errors " [syntax error]")))
1831 (and (org-string-nw-p errors) (org-trim errors)))))))
1833 (provide 'org-e-texinfo)
1834 ;;; org-e-texinfo.el ends here