org: correct package-version for org-structure-template-alist
[org-mode.git] / lisp / ox-texinfo.el
blobd11f387d7b4ceaa48ada982b19f9bf6be62bb10b
1 ;;; ox-texinfo.el --- Texinfo Back-End for Org Export Engine
3 ;; Copyright (C) 2012-2014 Free Software Foundation, Inc.
4 ;; Author: Jonathan Leech-Pepin <jonathan.leechpepin at gmail dot com>
5 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs 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 ;; GNU Emacs 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 GNU Emacs. 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 'texinfo "*Test Texinfo*") RET
31 ;; in an Org mode buffer then switch to the buffer to see the Texinfo
32 ;; export. See ox.el for more details on how this exporter works.
35 ;; It introduces nine new buffer keywords: "TEXINFO_CLASS",
36 ;; "TEXINFO_FILENAME", "TEXINFO_HEADER", "TEXINFO_POST_HEADER",
37 ;; "TEXINFO_DIR_CATEGORY", "TEXINFO_DIR_TITLE", "TEXINFO_DIR_DESC"
38 ;; "SUBTITLE" and "SUBAUTHOR".
41 ;; It introduces 1 new headline property keywords:
42 ;; "TEXINFO_MENU_TITLE" for optional menu titles.
44 ;; To include inline code snippets (for example for generating @kbd{}
45 ;; and @key{} commands), the following export-snippet keys are
46 ;; accepted:
48 ;; texinfo
49 ;; info
51 ;; You can add them for export snippets via any of the below:
53 ;; (add-to-list 'org-export-snippet-translation-alist
54 ;; '("info" . "texinfo"))
57 ;;; Code:
59 (eval-when-compile (require 'cl))
60 (require 'ox)
62 (defvar orgtbl-exp-regexp)
66 ;;; Define Back-End
68 (org-export-define-backend 'texinfo
69 '((bold . org-texinfo-bold)
70 (center-block . org-texinfo-center-block)
71 (clock . org-texinfo-clock)
72 (code . org-texinfo-code)
73 (comment . org-texinfo-comment)
74 (comment-block . org-texinfo-comment-block)
75 (drawer . org-texinfo-drawer)
76 (dynamic-block . org-texinfo-dynamic-block)
77 (entity . org-texinfo-entity)
78 (example-block . org-texinfo-example-block)
79 (export-block . org-texinfo-export-block)
80 (export-snippet . org-texinfo-export-snippet)
81 (fixed-width . org-texinfo-fixed-width)
82 (footnote-definition . org-texinfo-footnote-definition)
83 (footnote-reference . org-texinfo-footnote-reference)
84 (headline . org-texinfo-headline)
85 (inline-src-block . org-texinfo-inline-src-block)
86 (inlinetask . org-texinfo-inlinetask)
87 (italic . org-texinfo-italic)
88 (item . org-texinfo-item)
89 (keyword . org-texinfo-keyword)
90 (line-break . org-texinfo-line-break)
91 (link . org-texinfo-link)
92 (node-property . org-texinfo-node-property)
93 (paragraph . org-texinfo-paragraph)
94 (plain-list . org-texinfo-plain-list)
95 (plain-text . org-texinfo-plain-text)
96 (planning . org-texinfo-planning)
97 (property-drawer . org-texinfo-property-drawer)
98 (quote-block . org-texinfo-quote-block)
99 (radio-target . org-texinfo-radio-target)
100 (section . org-texinfo-section)
101 (special-block . org-texinfo-special-block)
102 (src-block . org-texinfo-src-block)
103 (statistics-cookie . org-texinfo-statistics-cookie)
104 (subscript . org-texinfo-subscript)
105 (superscript . org-texinfo-superscript)
106 (table . org-texinfo-table)
107 (table-cell . org-texinfo-table-cell)
108 (table-row . org-texinfo-table-row)
109 (target . org-texinfo-target)
110 (template . org-texinfo-template)
111 (timestamp . org-texinfo-timestamp)
112 (verbatim . org-texinfo-verbatim)
113 (verse-block . org-texinfo-verse-block))
114 :export-block "TEXINFO"
115 :filters-alist
116 '((:filter-headline . org-texinfo-filter-section-blank-lines)
117 (:filter-section . org-texinfo-filter-section-blank-lines))
118 :menu-entry
119 '(?i "Export to Texinfo"
120 ((?t "As TEXI file" org-texinfo-export-to-texinfo)
121 (?i "As INFO file" org-texinfo-export-to-info)))
122 :options-alist
123 '((:texinfo-filename "TEXINFO_FILENAME" nil org-texinfo-filename t)
124 (:texinfo-class "TEXINFO_CLASS" nil org-texinfo-default-class t)
125 (:texinfo-header "TEXINFO_HEADER" nil nil newline)
126 (:texinfo-post-header "TEXINFO_POST_HEADER" nil nil newline)
127 (:subtitle "SUBTITLE" nil nil newline)
128 (:subauthor "SUBAUTHOR" nil nil newline)
129 (:texinfo-dircat "TEXINFO_DIR_CATEGORY" nil nil t)
130 (:texinfo-dirtitle "TEXINFO_DIR_TITLE" nil nil t)
131 (:texinfo-dirdesc "TEXINFO_DIR_DESC" nil nil t)
132 ;; Other variables.
133 (:texinfo-coding-system nil nil org-texinfo-coding-system)
134 (:texinfo-classes nil nil org-texinfo-classes)
135 (:texinfo-format-headline-function nil nil org-texinfo-format-headline-function)
136 (:texinfo-node-description-column nil nil org-texinfo-node-description-column)
137 (:texinfo-active-timestamp-format nil nil org-texinfo-active-timestamp-format)
138 (:texinfo-inactive-timestamp-format nil nil org-texinfo-inactive-timestamp-format)
139 (:texinfo-diary-timestamp-format nil nil org-texinfo-diary-timestamp-format)
140 (:texinfo-link-with-unknown-path-format nil nil org-texinfo-link-with-unknown-path-format)
141 (:texinfo-tables-verbatim nil nil org-texinfo-tables-verbatim)
142 (:texinfo-table-scientific-notation nil nil org-texinfo-table-scientific-notation)
143 (:texinfo-def-table-markup nil nil org-texinfo-def-table-markup)
144 (:texinfo-text-markup-alist nil nil org-texinfo-text-markup-alist)
145 (:texinfo-format-drawer-function nil nil org-texinfo-format-drawer-function)
146 (:texinfo-format-inlinetask-function nil nil org-texinfo-format-inlinetask-function)
147 (:texinfo-info-process nil nil org-texinfo-info-process)
148 (:texinfo-logfiles-extensions nil nil org-texinfo-logfiles-extensions)
149 (:texinfo-remove-logfiles nil nil org-texinfo-remove-logfiles)))
153 ;;; User Configurable Variables
155 (defgroup org-export-texinfo nil
156 "Options for exporting Org mode files to Texinfo."
157 :tag "Org Export Texinfo"
158 :version "24.4"
159 :package-version '(Org . "8.0")
160 :group 'org-export)
162 ;;; Preamble
164 (defcustom org-texinfo-filename ""
165 "Default filename for Texinfo output."
166 :group 'org-export-texinfo
167 :type '(string :tag "Export Filename"))
169 (defcustom org-texinfo-coding-system nil
170 "Default document encoding for Texinfo output.
172 If `nil' it will default to `buffer-file-coding-system'."
173 :group 'org-export-texinfo
174 :type 'coding-system)
176 (defcustom org-texinfo-default-class "info"
177 "The default Texinfo class."
178 :group 'org-export-texinfo
179 :type '(string :tag "Texinfo class"))
181 (defcustom org-texinfo-classes
182 '(("info"
183 "\\input texinfo @c -*- texinfo -*-"
184 ("@chapter %s" . "@unnumbered %s")
185 ("@section %s" . "@unnumberedsec %s")
186 ("@subsection %s" . "@unnumberedsubsec %s")
187 ("@subsubsection %s" . "@unnumberedsubsubsec %s")))
188 "Alist of Texinfo classes and associated header and structure.
189 If #+Texinfo_CLASS is set in the buffer, use its value and the
190 associated information. Here is the structure of each cell:
192 \(class-name
193 header-string
194 \(numbered-section . unnumbered-section\)
195 ...\)
197 The sectioning structure
198 ------------------------
200 The sectioning structure of the class is given by the elements
201 following the header string. For each sectioning level, a number
202 of strings is specified. A %s formatter is mandatory in each
203 section string and will be replaced by the title of the section.
205 Instead of a list of sectioning commands, you can also specify
206 a function name. That function will be called with two
207 parameters, the \(reduced) level of the headline, and a predicate
208 non-nil when the headline should be numbered. It must return
209 a format string in which the section title will be added."
210 :group 'org-export-texinfo
211 :type '(repeat
212 (list (string :tag "Texinfo class")
213 (string :tag "Texinfo header")
214 (repeat :tag "Levels" :inline t
215 (choice
216 (cons :tag "Heading"
217 (string :tag " numbered")
218 (string :tag "unnumbered"))
219 (function :tag "Hook computing sectioning"))))))
221 ;;; Headline
223 (defcustom org-texinfo-format-headline-function 'ignore
224 "Function to format headline text.
226 This function will be called with 5 arguments:
227 TODO the todo keyword (string or nil).
228 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
229 PRIORITY the priority of the headline (integer or nil)
230 TEXT the main headline text (string).
231 TAGS the tags as a list of strings (list of strings or nil).
233 The function result will be used in the section format string.
235 As an example, one could set the variable to the following, in
236 order to reproduce the default set-up:
238 \(defun org-texinfo-format-headline (todo todo-type priority text tags)
239 \"Default format function for a headline.\"
240 \(concat (when todo
241 \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo))
242 \(when priority
243 \(format \"\\\\framebox{\\\\#%c} \" priority))
244 text
245 \(when tags
246 \(format \"\\\\hfill{}\\\\textsc{%s}\"
247 \(mapconcat 'identity tags \":\"))))"
248 :group 'org-export-texinfo
249 :type 'function)
251 ;;; Node listing (menu)
253 (defcustom org-texinfo-node-description-column 32
254 "Column at which to start the description in the node
255 listings.
257 If a node title is greater than this length, the description will
258 be placed after the end of the title."
259 :group 'org-export-texinfo
260 :type 'integer)
262 ;;; Footnotes
264 ;; Footnotes are inserted directly
266 ;;; Timestamps
268 (defcustom org-texinfo-active-timestamp-format "@emph{%s}"
269 "A printf format string to be applied to active timestamps."
270 :group 'org-export-texinfo
271 :type 'string)
273 (defcustom org-texinfo-inactive-timestamp-format "@emph{%s}"
274 "A printf format string to be applied to inactive timestamps."
275 :group 'org-export-texinfo
276 :type 'string)
278 (defcustom org-texinfo-diary-timestamp-format "@emph{%s}"
279 "A printf format string to be applied to diary timestamps."
280 :group 'org-export-texinfo
281 :type 'string)
283 ;;; Links
285 (defcustom org-texinfo-link-with-unknown-path-format "@indicateurl{%s}"
286 "Format string for links with unknown path type."
287 :group 'org-export-texinfo
288 :type 'string)
290 ;;; Tables
292 (defcustom org-texinfo-tables-verbatim nil
293 "When non-nil, tables are exported verbatim."
294 :group 'org-export-texinfo
295 :type 'boolean)
297 (defcustom org-texinfo-table-scientific-notation "%s\\,(%s)"
298 "Format string to display numbers in scientific notation.
299 The format should have \"%s\" twice, for mantissa and exponent
300 \(i.e. \"%s\\\\times10^{%s}\").
302 When nil, no transformation is made."
303 :group 'org-export-texinfo
304 :type '(choice
305 (string :tag "Format string")
306 (const :tag "No formatting")))
308 (defcustom org-texinfo-def-table-markup "@samp"
309 "Default setting for @table environments."
310 :group 'org-export-texinfo
311 :type 'string)
313 ;;; Text markup
315 (defcustom org-texinfo-text-markup-alist '((bold . "@strong{%s}")
316 (code . code)
317 (italic . "@emph{%s}")
318 (verbatim . verb)
319 (comment . "@c %s"))
320 "Alist of Texinfo expressions to convert text markup.
322 The key must be a symbol among `bold', `italic' and `comment'.
323 The value is a formatting string to wrap fontified text with.
325 Value can also be set to the following symbols: `verb' and
326 `code'. For the former, Org will use \"@verb\" to
327 create a format string and select a delimiter character that
328 isn't in the string. For the latter, Org will use \"@code\"
329 to typeset and try to protect special characters.
331 If no association can be found for a given markup, text will be
332 returned as-is."
333 :group 'org-export-texinfo
334 :type 'alist
335 :options '(bold code italic verbatim comment))
337 ;;; Drawers
339 (defcustom org-texinfo-format-drawer-function
340 (lambda (name contents) contents)
341 "Function called to format a drawer in Texinfo code.
343 The function must accept two parameters:
344 NAME the drawer name, like \"LOGBOOK\"
345 CONTENTS the contents of the drawer.
347 The function should return the string to be exported.
349 The default function simply returns the value of CONTENTS."
350 :group 'org-export-texinfo
351 :version "24.4"
352 :package-version '(Org . "8.3")
353 :type 'function)
355 ;;; Inlinetasks
357 (defcustom org-texinfo-format-inlinetask-function 'ignore
358 "Function called to format an inlinetask in Texinfo code.
360 The function must accept six parameters:
361 TODO the todo keyword, as a string
362 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
363 PRIORITY the inlinetask priority, as a string
364 NAME the inlinetask name, as a string.
365 TAGS the inlinetask tags, as a list of strings.
366 CONTENTS the contents of the inlinetask, as a string.
368 The function should return the string to be exported.
370 For example, the variable could be set to the following function
371 in order to mimic default behavior:
373 \(defun org-texinfo-format-inlinetask \(todo type priority name tags contents\)
374 \"Format an inline task element for Texinfo export.\"
375 \(let ((full-title
376 \(concat
377 \(when todo
378 \(format \"@strong{%s} \" todo))
379 \(when priority (format \"#%c \" priority))
380 title
381 \(when tags
382 \(format \":%s:\"
383 \(mapconcat 'identity tags \":\")))))
384 \(format (concat \"@center %s\n\n\"
385 \"%s\"
386 \"\n\"))
387 full-title contents))"
388 :group 'org-export-texinfo
389 :type 'function)
391 ;;; Src blocks
393 ;; Src Blocks are example blocks, except for LISP
395 ;;; Compilation
397 (defcustom org-texinfo-info-process
398 '("makeinfo %f")
399 "Commands to process a Texinfo file to an INFO file.
400 This is list of strings, each of them will be given to the shell
401 as a command. %f in the command will be replaced by the full
402 file name, %b by the file base name \(i.e without extension) and
403 %o by the base directory of the file."
404 :group 'org-export-texinfo
405 :type '(repeat :tag "Shell command sequence"
406 (string :tag "Shell command")))
408 (defcustom org-texinfo-logfiles-extensions
409 '("aux" "toc" "cp" "fn" "ky" "pg" "tp" "vr")
410 "The list of file extensions to consider as Texinfo logfiles.
411 The logfiles will be remove if `org-texinfo-remove-logfiles' is
412 non-nil."
413 :group 'org-export-texinfo
414 :type '(repeat (string :tag "Extension")))
416 (defcustom org-texinfo-remove-logfiles t
417 "Non-nil means remove the logfiles produced by compiling a Texinfo file.
418 By default, logfiles are files with these extensions: .aux, .toc,
419 .cp, .fn, .ky, .pg and .tp. To define the set of logfiles to remove,
420 set `org-texinfo-logfiles-extensions'."
421 :group 'org-export-latex
422 :type 'boolean)
425 ;;; Constants
426 (defconst org-texinfo-max-toc-depth 4
427 "Maximum depth for creation of detailed menu listings. Beyond
428 this depth Texinfo will not recognize the nodes and will cause
429 errors. Left as a constant in case this value ever changes.")
431 (defconst org-texinfo-supported-coding-systems
432 '("US-ASCII" "UTF-8" "ISO-8859-15" "ISO-8859-1" "ISO-8859-2" "koi8-r" "koi8-u")
433 "List of coding systems supported by Texinfo, as strings.
434 Specified coding system will be matched against these strings.
435 If two strings share the same prefix (e.g. \"ISO-8859-1\" and
436 \"ISO-8859-15\"), the most specific one has to be listed first.")
439 ;;; Internal Functions
441 (defun org-texinfo-filter-section-blank-lines (headline back-end info)
442 "Filter controlling number of blank lines after a section."
443 (let ((blanks (make-string 2 ?\n)))
444 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline)))
446 (defun org-texinfo--find-verb-separator (s)
447 "Return a character not used in string S.
448 This is used to choose a separator for constructs like \\verb."
449 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
450 (loop for c across ll
451 when (not (string-match (regexp-quote (char-to-string c)) s))
452 return (char-to-string c))))
454 (defun org-texinfo--make-option-string (options)
455 "Return a comma separated string of keywords and values.
456 OPTIONS is an alist where the key is the options keyword as
457 a string, and the value a list containing the keyword value, or
458 nil."
459 (mapconcat (lambda (pair)
460 (concat (first pair)
461 (when (> (length (second pair)) 0)
462 (concat "=" (second pair)))))
463 options
464 ","))
466 (defun org-texinfo--text-markup (text markup)
467 "Format TEXT depending on MARKUP text markup.
468 See `org-texinfo-text-markup-alist' for details."
469 (let ((fmt (cdr (assq markup org-texinfo-text-markup-alist))))
470 (cond
471 ;; No format string: Return raw text.
472 ((not fmt) text)
473 ((eq 'verb fmt)
474 (let ((separator (org-texinfo--find-verb-separator text)))
475 (concat "@verb{" separator text separator "}")))
476 ((eq 'code fmt)
477 (let ((start 0)
478 (rtn "")
479 char)
480 (while (string-match "[@{}]" text)
481 (setq char (match-string 0 text))
482 (if (> (match-beginning 0) 0)
483 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
484 (setq text (substring text (1+ (match-beginning 0))))
485 (setq char (concat "@" char)
486 rtn (concat rtn char)))
487 (setq text (concat rtn text)
488 fmt "@code{%s}")
489 (format fmt text)))
490 ;; Else use format string.
491 (t (format fmt text)))))
493 (defun org-texinfo--get-node (headline info)
494 "Return node entry associated to HEADLINE.
495 INFO is a plist used as a communication channel."
496 (let ((menu-title (org-export-get-alt-title headline info)))
497 (org-texinfo--sanitize-menu
498 (replace-regexp-in-string
499 "%" "%%"
500 (if menu-title (org-export-data menu-title info)
501 (org-texinfo--sanitize-headline
502 (org-element-property :title headline) info))))))
504 ;;; Headline sanitizing
506 (defun org-texinfo--sanitize-headline (headline info)
507 "Remove all formatting from the text of a headline for use in
508 node and menu listing."
509 (mapconcat 'identity
510 (org-texinfo--sanitize-headline-contents headline info) " "))
512 (defun org-texinfo--sanitize-headline-contents (headline info)
513 "Retrieve the content of the headline.
515 Any content that can contain further formatting is checked
516 recursively, to ensure that nested content is also properly
517 retrieved."
518 (loop for contents in headline append
519 (cond
520 ;; already a string
521 ((stringp contents)
522 (list (replace-regexp-in-string " $" "" contents)))
523 ;; Is exported as-is (value)
524 ((org-element-map contents '(verbatim code)
525 (lambda (value) (org-element-property :value value)) info))
526 ;; Has content and recurse into the content
527 ((org-element-contents contents)
528 (org-texinfo--sanitize-headline-contents
529 (org-element-contents contents) info)))))
531 ;;; Menu sanitizing
533 (defun org-texinfo--sanitize-menu (title)
534 "Remove invalid characters from TITLE for use in menus and
535 nodes.
537 Based on Texinfo specifications, the following must be removed:
538 @ { } ( ) : . ,"
539 (replace-regexp-in-string "[@{}():,.]" "" title))
541 ;;; Content sanitizing
543 (defun org-texinfo--sanitize-content (text)
544 "Ensure characters are properly escaped when used in headlines or blocks.
546 Escape characters are: @ { }"
547 (replace-regexp-in-string "\\\([@{}]\\\)" "@\\1" text))
549 ;;; Menu creation
551 (defun org-texinfo--build-menu (tree level info &optional detailed)
552 "Create the @menu/@end menu information from TREE at headline
553 level LEVEL.
555 TREE contains the parse-tree to work with, either of the entire
556 document or of a specific parent headline. LEVEL indicates what
557 level of headlines to look at when generating the menu. INFO is
558 a plist containing contextual information.
560 Detailed determines whether to build a single level of menu, or
561 recurse into all children as well."
562 (let ((menu (org-texinfo--generate-menu-list tree level info))
563 output text-menu)
564 (cond
565 (detailed
566 ;; Looping is done within the menu generation.
567 (setq text-menu (org-texinfo--generate-detailed menu level info)))
569 (setq text-menu (org-texinfo--generate-menu-items menu info))))
570 (when text-menu
571 (setq output (org-texinfo--format-menu text-menu))
572 (mapconcat 'identity output "\n"))))
574 (defun org-texinfo--generate-detailed (menu level info)
575 "Generate a detailed listing of all subheadings within MENU starting at LEVEL.
577 MENU is the parse-tree to work with. LEVEL is the starting level
578 for the menu headlines and from which recursion occurs. INFO is
579 a plist containing contextual information."
580 (when level
581 (let ((max-depth (min org-texinfo-max-toc-depth
582 (plist-get info :headline-levels))))
583 (when (> max-depth level)
584 (loop for headline in menu append
585 (let* ((title (org-texinfo--menu-headlines headline info))
586 ;; Create list of menu entries for the next level
587 (sublist (org-texinfo--generate-menu-list
588 headline (1+ level) info))
589 ;; Generate the menu items for that level. If
590 ;; there are none omit that heading completely,
591 ;; otherwise join the title to it's related entries.
592 (submenu (if (org-texinfo--generate-menu-items sublist info)
593 (append (list title)
594 (org-texinfo--generate-menu-items sublist info))
595 'nil))
596 ;; Start the process over the next level down.
597 (recursion (org-texinfo--generate-detailed sublist (1+ level) info)))
598 (setq recursion (append submenu recursion))
599 recursion))))))
601 (defun org-texinfo--generate-menu-list (tree level info)
602 "Generate the list of headlines that are within a given level
603 of the tree for further formatting.
605 TREE is the parse-tree containing the headlines. LEVEL is the
606 headline level to generate a list of. INFO is a plist holding
607 contextual information."
608 (org-element-map tree 'headline
609 (lambda (head)
610 (and (= (org-export-get-relative-level head info) level)
611 ;; Do not take note of footnotes or copying headlines.
612 (not (org-element-property :COPYING head))
613 (not (org-element-property :footnote-section-p head))
614 ;; Collect headline.
615 head))
616 info))
618 (defun org-texinfo--generate-menu-items (items info)
619 "Generate a list of headline information from the listing ITEMS.
621 ITEMS is a list of the headlines to be converted into entries.
622 INFO is a plist containing contextual information.
624 Returns a list containing the following information from each
625 headline: length, title, description. This is used to format the
626 menu using `org-texinfo--format-menu'."
627 (loop for headline in items collect
628 (let* ((menu-title (org-texinfo--sanitize-menu
629 (org-export-data
630 (org-export-get-alt-title headline info)
631 info)))
632 (title (org-texinfo--sanitize-menu
633 (org-texinfo--sanitize-headline
634 (org-element-property :title headline) info)))
635 (descr (org-export-data
636 (org-element-property :DESCRIPTION headline)
637 info))
638 (menu-entry (if (string= "" menu-title) title menu-title))
639 (len (length menu-entry))
640 (output (list len menu-entry descr)))
641 output)))
643 (defun org-texinfo--menu-headlines (headline info)
644 "Retrieve the title from HEADLINE.
646 INFO is a plist holding contextual information.
648 Return the headline as a list of (length title description) with
649 length of -1 and nil description. This is used in
650 `org-texinfo--format-menu' to identify headlines as opposed to
651 entries."
652 (let ((title (org-export-data
653 (org-element-property :title headline) info)))
654 (list -1 title 'nil)))
656 (defun org-texinfo--format-menu (text-menu)
657 "Format the TEXT-MENU items to be properly printed in the menu.
659 Each entry in the menu should be provided as (length title
660 description).
662 Headlines in the detailed menu are given length -1 to ensure they
663 are never confused with other entries. They also have no
664 description.
666 Other menu items are output as:
667 Title:: description
669 With the spacing between :: and description based on the length
670 of the longest menu entry."
672 (let (output)
673 (setq output
674 (mapcar (lambda (name)
675 (let* ((title (nth 1 name))
676 (desc (nth 2 name))
677 (length (nth 0 name))
678 (column (max
679 ;;6 is "* " ":: " for inserted text
680 length
682 org-texinfo-node-description-column
683 6)))
684 (spacing (- column length)
686 (if (> length -1)
687 (concat "* " title ":: "
688 (make-string spacing ?\s)
689 (if desc
690 (concat desc)))
691 (concat "\n" title "\n"))))
692 text-menu))
693 output))
695 ;;; Template
697 (defun org-texinfo-template (contents info)
698 "Return complete document string after Texinfo conversion.
699 CONTENTS is the transcoded contents string. INFO is a plist
700 holding export options."
701 (let* ((title (org-export-data (plist-get info :title) info))
702 (info-filename (or (plist-get info :texinfo-filename)
703 (file-name-nondirectory
704 (org-export-output-file-name ".info"))))
705 (author (org-export-data (plist-get info :author) info))
706 (lang (org-export-data (plist-get info :language) info))
707 (texinfo-header (plist-get info :texinfo-header))
708 (texinfo-post-header (plist-get info :texinfo-post-header))
709 (subtitle (plist-get info :subtitle))
710 (subauthor (plist-get info :subauthor))
711 (class (plist-get info :texinfo-class))
712 (header (nth 1 (assoc class org-texinfo-classes)))
713 (copying
714 (org-element-map (plist-get info :parse-tree) 'headline
715 (lambda (hl) (and (org-element-property :COPYING hl) hl)) info t))
716 (dircat (plist-get info :texinfo-dircat))
717 (dirtitle (plist-get info :texinfo-dirtitle))
718 (dirdesc (plist-get info :texinfo-dirdesc))
719 ;; Spacing to align description (column 32 - 3 for `* ' and
720 ;; `.' in text.
721 (dirspacing (- 29 (length dirtitle)))
722 (menu (org-texinfo-make-menu info 'main))
723 (detail-menu (org-texinfo-make-menu info 'detailed)))
724 (concat
725 ;; Header
726 header "\n"
727 "@c %**start of header\n"
728 ;; Filename and Title
729 "@setfilename " info-filename "\n"
730 "@settitle " title "\n"
731 ;; Coding system.
732 (format
733 "@documentencoding %s\n"
734 (catch 'coding-system
735 (let ((case-fold-search t)
736 (name (symbol-name (or org-texinfo-coding-system
737 buffer-file-coding-system))))
738 (dolist (system org-texinfo-supported-coding-systems "UTF-8")
739 (when (org-string-match-p (regexp-quote system) name)
740 (throw 'coding-system system))))))
741 "\n"
742 (format "@documentlanguage %s\n" lang)
743 "\n\n"
744 "@c Version and Contact Info\n"
745 "@set AUTHOR " author "\n"
747 ;; Additional Header Options set by `#+TEXINFO_HEADER
748 (if texinfo-header
749 (concat "\n"
750 texinfo-header
751 "\n"))
753 "@c %**end of header\n"
754 "@finalout\n"
755 "\n\n"
757 ;; Additional Header Options set by #+TEXINFO_POST_HEADER
758 (if texinfo-post-header
759 (concat "\n"
760 texinfo-post-header
761 "\n"))
763 ;; Copying
764 "@copying\n"
765 ;; Only export the content of the headline, do not need the
766 ;; initial headline.
767 (org-export-data (nth 2 copying) info)
768 "@end copying\n"
769 "\n\n"
771 ;; Info directory information
772 ;; Only supply if both title and category are provided
773 (if (and dircat dirtitle)
774 (concat "@dircategory " dircat "\n"
775 "@direntry\n"
776 "* " dirtitle "."
777 (make-string dirspacing ?\s)
778 dirdesc "\n"
779 "@end direntry\n"))
780 "\n\n"
782 ;; Title
783 "@titlepage\n"
784 "@title " title "\n\n"
785 (if subtitle
786 (concat "@subtitle " subtitle "\n"))
787 "@author " author "\n"
788 (if subauthor
789 (concat subauthor "\n"))
790 "\n"
791 "@c The following two commands start the copyright page.\n"
792 "@page\n"
793 "@vskip 0pt plus 1filll\n"
794 "@insertcopying\n"
795 "@end titlepage\n\n"
796 "@c Output the table of contents at the beginning.\n"
797 "@contents\n\n"
799 ;; Configure Top Node when not for Tex
800 "@ifnottex\n"
801 "@node Top\n"
802 "@top " title " Manual\n"
803 "@insertcopying\n"
804 "@end ifnottex\n\n"
806 ;; Do not output menus if they are empty
807 (if menu
808 ;; Menu
809 (concat "@menu\n"
810 menu
811 "\n\n"
812 ;; Detailed Menu
813 (if detail-menu
814 (concat "@detailmenu\n"
815 " --- The Detailed Node Listing ---\n"
816 detail-menu
817 "\n\n"
818 "@end detailmenu\n"))
819 "@end menu\n"))
820 "\n\n"
822 ;; Document's body.
823 contents
824 "\n"
825 ;; Creator.
826 (let ((creator-info (plist-get info :with-creator)))
827 (cond
828 ((not creator-info) "")
829 ((eq creator-info 'comment)
830 (format "@c %s\n" (plist-get info :creator)))
831 (t (concat (plist-get info :creator) "\n"))))
832 ;; Document end.
833 "\n@bye")))
837 ;;; Transcode Functions
839 ;;; Bold
841 (defun org-texinfo-bold (bold contents info)
842 "Transcode BOLD from Org to Texinfo.
843 CONTENTS is the text with bold markup. INFO is a plist holding
844 contextual information."
845 (org-texinfo--text-markup contents 'bold))
847 ;;; Center Block
849 (defun org-texinfo-center-block (center-block contents info)
850 "Transcode a CENTER-BLOCK element from Org to Texinfo.
851 CONTENTS holds the contents of the block. INFO is a plist used
852 as a communication channel."
853 contents)
855 ;;; Clock
857 (defun org-texinfo-clock (clock contents info)
858 "Transcode a CLOCK element from Org to Texinfo.
859 CONTENTS is nil. INFO is a plist holding contextual
860 information."
861 (concat
862 "@noindent"
863 (format "@strong{%s} " org-clock-string)
864 (format org-texinfo-inactive-timestamp-format
865 (concat (org-translate-time
866 (org-element-property :raw-value
867 (org-element-property :value clock)))
868 (let ((time (org-element-property :duration clock)))
869 (and time (format " (%s)" time)))))
870 "@*"))
872 ;;; Code
874 (defun org-texinfo-code (code contents info)
875 "Transcode a CODE object from Org to Texinfo.
876 CONTENTS is nil. INFO is a plist used as a communication
877 channel."
878 (org-texinfo--text-markup (org-element-property :value code) 'code))
880 ;;; Comment
882 (defun org-texinfo-comment (comment contents info)
883 "Transcode a COMMENT object from Org to Texinfo.
884 CONTENTS is the text in the comment. INFO is a plist holding
885 contextual information."
886 (org-texinfo--text-markup (org-element-property :value comment) 'comment))
888 ;;; Comment Block
890 (defun org-texinfo-comment-block (comment-block contents info)
891 "Transcode a COMMENT-BLOCK object from Org to Texinfo.
892 CONTENTS is the text within the block. INFO is a plist holding
893 contextual information."
894 (format "@ignore\n%s@end ignore" (org-element-property :value comment-block)))
896 ;;; Drawer
898 (defun org-texinfo-drawer (drawer contents info)
899 "Transcode a DRAWER element from Org to Texinfo.
900 CONTENTS holds the contents of the block. INFO is a plist
901 holding contextual information."
902 (let* ((name (org-element-property :drawer-name drawer))
903 (output (funcall org-texinfo-format-drawer-function
904 name contents)))
905 output))
907 ;;; Dynamic Block
909 (defun org-texinfo-dynamic-block (dynamic-block contents info)
910 "Transcode a DYNAMIC-BLOCK element from Org to Texinfo.
911 CONTENTS holds the contents of the block. INFO is a plist
912 holding contextual information. See `org-export-data'."
913 contents)
915 ;;; Entity
917 (defun org-texinfo-entity (entity contents info)
918 "Transcode an ENTITY object from Org to Texinfo.
919 CONTENTS are the definition itself. INFO is a plist holding
920 contextual information."
921 (let ((ent (org-element-property :latex entity)))
922 (if (org-element-property :latex-math-p entity) (format "@math{%s}" ent) ent)))
924 ;;; Example Block
926 (defun org-texinfo-example-block (example-block contents info)
927 "Transcode an EXAMPLE-BLOCK element from Org to Texinfo.
928 CONTENTS is nil. INFO is a plist holding contextual
929 information."
930 (format "@verbatim\n%s@end verbatim"
931 (org-export-format-code-default example-block info)))
933 ;;; Export Block
935 (defun org-texinfo-export-block (export-block contents info)
936 "Transcode a EXPORT-BLOCK element from Org to Texinfo.
937 CONTENTS is nil. INFO is a plist holding contextual information."
938 (when (string= (org-element-property :type export-block) "TEXINFO")
939 (org-remove-indentation (org-element-property :value export-block))))
941 ;;; Export Snippet
943 (defun org-texinfo-export-snippet (export-snippet contents info)
944 "Transcode a EXPORT-SNIPPET object from Org to Texinfo.
945 CONTENTS is nil. INFO is a plist holding contextual information."
946 (when (eq (org-export-snippet-backend export-snippet) 'texinfo)
947 (org-element-property :value export-snippet)))
949 ;;; Fixed Width
951 (defun org-texinfo-fixed-width (fixed-width contents info)
952 "Transcode a FIXED-WIDTH element from Org to Texinfo.
953 CONTENTS is nil. INFO is a plist holding contextual information."
954 (format "@example\n%s\n@end example"
955 (org-remove-indentation
956 (org-texinfo--sanitize-content
957 (org-element-property :value fixed-width)))))
959 ;;; Footnote Reference
962 (defun org-texinfo-footnote-reference (footnote contents info)
963 "Create a footnote reference for FOOTNOTE.
965 FOOTNOTE is the footnote to define. CONTENTS is nil. INFO is a
966 plist holding contextual information."
967 (let ((def (org-export-get-footnote-definition footnote info)))
968 (format "@footnote{%s}"
969 (org-trim (org-export-data def info)))))
971 ;;; Headline
973 (defun org-texinfo-headline (headline contents info)
974 "Transcode a HEADLINE element from Org to Texinfo.
975 CONTENTS holds the contents of the headline. INFO is a plist
976 holding contextual information."
977 (let* ((class (plist-get info :texinfo-class))
978 (level (org-export-get-relative-level headline info))
979 (numberedp (org-export-numbered-headline-p headline info))
980 (class-sectioning (assoc class org-texinfo-classes))
981 ;; Find the index type, if any
982 (index (org-element-property :INDEX headline))
983 ;; Check if it is an appendix
984 (appendix (org-element-property :APPENDIX headline))
985 ;; Retrieve headline text
986 (text (org-texinfo--sanitize-headline
987 (org-element-property :title headline) info))
988 ;; Create node info, to insert it before section formatting.
989 ;; Use custom menu title if present
990 (node (format "@node %s\n" (org-texinfo--get-node headline info)))
991 ;; Menus must be generated with first child, otherwise they
992 ;; will not nest properly
993 (menu (let* ((first (org-export-first-sibling-p headline info))
994 (parent (org-export-get-parent-headline headline))
995 (title (org-texinfo--sanitize-headline
996 (org-element-property :title parent) info))
997 heading listing
998 (tree (plist-get info :parse-tree)))
999 (if first
1000 (org-element-map (plist-get info :parse-tree) 'headline
1001 (lambda (ref)
1002 (if (member title (org-element-property :title ref))
1003 (push ref heading)))
1004 info t))
1005 (setq listing (org-texinfo--build-menu
1006 (car heading) level info))
1007 (if listing
1008 (setq listing (replace-regexp-in-string
1009 "%" "%%" listing)
1010 listing (format
1011 "\n@menu\n%s\n@end menu\n\n" listing))
1012 'nil)))
1013 ;; Section formatting will set two placeholders: one for the
1014 ;; title and the other for the contents.
1015 (section-fmt
1016 (let ((sec (if (and (symbolp (nth 2 class-sectioning))
1017 (fboundp (nth 2 class-sectioning)))
1018 (funcall (nth 2 class-sectioning) level numberedp)
1019 (nth (1+ level) class-sectioning))))
1020 (cond
1021 ;; No section available for that LEVEL.
1022 ((not sec) nil)
1023 ;; Section format directly returned by a function.
1024 ((stringp sec) sec)
1025 ;; (numbered-section . unnumbered-section)
1026 ((not (consp (cdr sec)))
1027 (cond
1028 ;;If an index, always unnumbered
1029 (index
1030 (concat menu node (cdr sec) "\n%s"))
1031 (appendix
1032 (concat menu node (replace-regexp-in-string
1033 "unnumbered"
1034 "appendix"
1035 (cdr sec)) "\n%s"))
1036 ;; Otherwise number as needed.
1038 (concat menu node
1039 (funcall
1040 (if numberedp #'car #'cdr) sec) "\n%s")))))))
1041 (todo
1042 (and (plist-get info :with-todo-keywords)
1043 (let ((todo (org-element-property :todo-keyword headline)))
1044 (and todo (org-export-data todo info)))))
1045 (todo-type (and todo (org-element-property :todo-type headline)))
1046 (tags (and (plist-get info :with-tags)
1047 (org-export-get-tags headline info)))
1048 (priority (and (plist-get info :with-priority)
1049 (org-element-property :priority headline)))
1050 ;; Create the headline text along with a no-tag version. The
1051 ;; latter is required to remove tags from table of contents.
1052 (full-text (org-texinfo--sanitize-content
1053 (if (not (eq org-texinfo-format-headline-function 'ignore))
1054 ;; User-defined formatting function.
1055 (funcall org-texinfo-format-headline-function
1056 todo todo-type priority text tags)
1057 ;; Default formatting.
1058 (concat
1059 (when todo
1060 (format "@strong{%s} " todo))
1061 (when priority (format "@emph{#%s} " priority))
1062 text
1063 (when tags
1064 (format " :%s:"
1065 (mapconcat 'identity tags ":")))))))
1066 (full-text-no-tag
1067 (org-texinfo--sanitize-content
1068 (if (not (eq org-texinfo-format-headline-function 'ignore))
1069 ;; User-defined formatting function.
1070 (funcall org-texinfo-format-headline-function
1071 todo todo-type priority text nil)
1072 ;; Default formatting.
1073 (concat
1074 (when todo (format "@strong{%s} " todo))
1075 (when priority (format "@emph{#%c} " priority))
1076 text))))
1077 (pre-blanks
1078 (make-string (org-element-property :pre-blank headline) 10)))
1079 (cond
1080 ;; Case 1: This is a footnote section: ignore it.
1081 ((org-element-property :footnote-section-p headline) nil)
1082 ;; Case 2: This is the `copying' section: ignore it
1083 ;; This is used elsewhere.
1084 ((org-element-property :COPYING headline) nil)
1085 ;; Case 3: An index. If it matches one of the known indexes,
1086 ;; print it as such following the contents, otherwise
1087 ;; print the contents and leave the index up to the user.
1088 (index
1089 (format
1090 section-fmt full-text
1091 (concat pre-blanks contents "\n"
1092 (if (member index '("cp" "fn" "ky" "pg" "tp" "vr"))
1093 (concat "@printindex " index)))))
1094 ;; Case 4: This is a deep sub-tree: export it as a list item.
1095 ;; Also export as items headlines for which no section
1096 ;; format has been found.
1097 ((or (not section-fmt) (org-export-low-level-p headline info))
1098 ;; Build the real contents of the sub-tree.
1099 (let ((low-level-body
1100 (concat
1101 ;; If the headline is the first sibling, start a list.
1102 (when (org-export-first-sibling-p headline info)
1103 (format "@%s\n" (if numberedp 'enumerate 'itemize)))
1104 ;; Itemize headline
1105 "@item\n" full-text "\n" pre-blanks contents)))
1106 ;; If headline is not the last sibling simply return
1107 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1108 ;; blank line.
1109 (if (not (org-export-last-sibling-p headline info)) low-level-body
1110 (replace-regexp-in-string
1111 "[ \t\n]*\\'"
1112 (format "\n@end %s" (if numberedp 'enumerate 'itemize))
1113 low-level-body))))
1114 ;; Case 5: Standard headline. Export it as a section.
1116 (cond
1117 ((not (and tags (eq (plist-get info :with-tags) 'not-in-toc)))
1118 ;; Regular section. Use specified format string.
1119 (format (replace-regexp-in-string "%]" "%%]" section-fmt) full-text
1120 (concat pre-blanks contents)))
1121 ((string-match "\\`@\\(.*?\\){" section-fmt)
1122 ;; If tags should be removed from table of contents, insert
1123 ;; title without tags as an alternative heading in sectioning
1124 ;; command.
1125 (format (replace-match (concat (match-string 1 section-fmt) "[%s]")
1126 nil nil section-fmt 1)
1127 ;; Replace square brackets with parenthesis since
1128 ;; square brackets are not supported in optional
1129 ;; arguments.
1130 (replace-regexp-in-string
1131 "\\[" "("
1132 (replace-regexp-in-string
1133 "\\]" ")"
1134 full-text-no-tag))
1135 full-text
1136 (concat pre-blanks contents)))
1138 ;; Impossible to add an alternative heading. Fallback to
1139 ;; regular sectioning format string.
1140 (format (replace-regexp-in-string "%]" "%%]" section-fmt) full-text
1141 (concat pre-blanks contents))))))))
1143 ;;; Inline Src Block
1145 (defun org-texinfo-inline-src-block (inline-src-block contents info)
1146 "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
1147 CONTENTS holds the contents of the item. INFO is a plist holding
1148 contextual information."
1149 (let* ((code (org-element-property :value inline-src-block))
1150 (separator (org-texinfo--find-verb-separator code)))
1151 (concat "@verb{" separator code separator "}")))
1153 ;;; Inlinetask
1155 (defun org-texinfo-inlinetask (inlinetask contents info)
1156 "Transcode an INLINETASK element from Org to Texinfo.
1157 CONTENTS holds the contents of the block. INFO is a plist
1158 holding contextual information."
1159 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1160 (todo (and (plist-get info :with-todo-keywords)
1161 (let ((todo (org-element-property :todo-keyword inlinetask)))
1162 (and todo (org-export-data todo info)))))
1163 (todo-type (org-element-property :todo-type inlinetask))
1164 (tags (and (plist-get info :with-tags)
1165 (org-export-get-tags inlinetask info)))
1166 (priority (and (plist-get info :with-priority)
1167 (org-element-property :priority inlinetask))))
1168 ;; If `org-texinfo-format-inlinetask-function' is provided, call it
1169 ;; with appropriate arguments.
1170 (if (not (eq org-texinfo-format-inlinetask-function 'ignore))
1171 (funcall org-texinfo-format-inlinetask-function
1172 todo todo-type priority title tags contents)
1173 ;; Otherwise, use a default template.
1174 (let ((full-title
1175 (concat
1176 (when todo (format "@strong{%s} " todo))
1177 (when priority (format "#%c " priority))
1178 title
1179 (when tags (format ":%s:"
1180 (mapconcat 'identity tags ":"))))))
1181 (format (concat "@center %s\n\n"
1182 "%s"
1183 "\n")
1184 full-title contents)))))
1186 ;;; Italic
1188 (defun org-texinfo-italic (italic contents info)
1189 "Transcode ITALIC from Org to Texinfo.
1190 CONTENTS is the text with italic markup. INFO is a plist holding
1191 contextual information."
1192 (org-texinfo--text-markup contents 'italic))
1194 ;;; Item
1196 (defun org-texinfo-item (item contents info)
1197 "Transcode an ITEM element from Org to Texinfo.
1198 CONTENTS holds the contents of the item. INFO is a plist holding
1199 contextual information."
1200 (let* ((tag (org-element-property :tag item))
1201 (desc (org-export-data tag info)))
1202 (concat "\n@item " (if tag desc) "\n"
1203 (and contents (org-trim contents)) "\n")))
1205 ;;; Keyword
1207 (defun org-texinfo-keyword (keyword contents info)
1208 "Transcode a KEYWORD element from Org to Texinfo.
1209 CONTENTS is nil. INFO is a plist holding contextual information."
1210 (let ((key (org-element-property :key keyword))
1211 (value (org-element-property :value keyword)))
1212 (cond
1213 ((string= key "TEXINFO") value)
1214 ((string= key "CINDEX") (format "@cindex %s" value))
1215 ((string= key "FINDEX") (format "@findex %s" value))
1216 ((string= key "KINDEX") (format "@kindex %s" value))
1217 ((string= key "PINDEX") (format "@pindex %s" value))
1218 ((string= key "TINDEX") (format "@tindex %s" value))
1219 ((string= key "VINDEX") (format "@vindex %s" value)))))
1221 ;;; Line Break
1223 (defun org-texinfo-line-break (line-break contents info)
1224 "Transcode a LINE-BREAK object from Org to Texinfo.
1225 CONTENTS is nil. INFO is a plist holding contextual information."
1226 "@*\n")
1228 ;;; Link
1230 (defun org-texinfo-link (link desc info)
1231 "Transcode a LINK object from Org to Texinfo.
1233 DESC is the description part of the link, or the empty string.
1234 INFO is a plist holding contextual information. See
1235 `org-export-data'."
1236 (let* ((type (org-element-property :type link))
1237 (raw-path (org-element-property :path link))
1238 ;; Ensure DESC really exists, or set it to nil.
1239 (desc (and (not (string= desc "")) desc))
1240 (path (cond
1241 ((member type '("http" "https" "ftp"))
1242 (concat type ":" raw-path))
1243 ((and (string= type "file") (file-name-absolute-p raw-path))
1244 (concat "file:" raw-path))
1245 (t raw-path)))
1246 (email (if (string= type "mailto")
1247 (let ((text (replace-regexp-in-string
1248 "@" "@@" raw-path)))
1249 (concat text (if desc (concat "," desc))))))
1250 protocol)
1251 (cond
1252 ;; Links pointing to a headline: Find destination and build
1253 ;; appropriate referencing command.
1254 ((member type '("custom-id" "id"))
1255 (let ((destination (org-export-resolve-id-link link info)))
1256 (case (org-element-type destination)
1257 ;; Id link points to an external file.
1258 (plain-text
1259 (if desc (format "@uref{file://%s,%s}" destination desc)
1260 (format "@uref{file://%s}" destination)))
1261 ;; LINK points to a headline. Use the headline as the NODE target
1262 (headline
1263 (format "@ref{%s,%s}"
1264 (org-texinfo--get-node destination info)
1265 (or desc "")))
1266 (otherwise
1267 (let ((path (org-export-solidify-link-text path)))
1268 (if (not desc) (format "@ref{%s}" path)
1269 (format "@ref{%s,,%s}" path desc)))))))
1270 ((member type '("info"))
1271 (let* ((info-path (split-string path "[:#]"))
1272 (info-manual (car info-path))
1273 (info-node (or (cadr info-path) "top"))
1274 (title (or desc "")))
1275 (format "@ref{%s,%s,,%s,}" info-node title info-manual)))
1276 ((member type '("fuzzy"))
1277 (let ((destination (org-export-resolve-fuzzy-link link info)))
1278 (case (org-element-type destination)
1279 ;; Id link points to an external file.
1280 (plain-text
1281 (if desc (format "@uref{file://%s,%s}" destination desc)
1282 (format "@uref{file://%s}" destination)))
1283 ;; LINK points to a headline. Use the headline as the NODE target
1284 (headline
1285 (format "@ref{%s,%s}"
1286 (org-texinfo--get-node destination info)
1287 (or desc "")))
1288 (otherwise
1289 (let ((path (org-export-solidify-link-text path)))
1290 (if (not desc) (format "@ref{%s}" path)
1291 (format "@ref{%s,,%s}" path desc)))))))
1292 ;; Special case for email addresses
1293 (email
1294 (format "@email{%s}" email))
1295 ;; External link with a description part.
1296 ((and path desc) (format "@uref{%s,%s}" path desc))
1297 ;; External link without a description part.
1298 (path (format "@uref{%s}" path))
1299 ;; No path, only description. Try to do something useful.
1300 (t (format org-texinfo-link-with-unknown-path-format desc)))))
1303 ;;; Menu
1305 (defun org-texinfo-make-menu (info level)
1306 "Create the menu for inclusion in the texifo document.
1308 INFO is the parsed buffer that contains the headlines. LEVEL
1309 determines whether to make the main menu, or the detailed menu.
1311 This is only used for generating the primary menu. In-Node menus
1312 are generated directly."
1313 (let ((parse (plist-get info :parse-tree)))
1314 (cond
1315 ;; Generate the main menu
1316 ((eq level 'main) (org-texinfo--build-menu parse 1 info))
1317 ;; Generate the detailed (recursive) menu
1318 ((eq level 'detailed)
1319 ;; Requires recursion
1320 ;;(org-texinfo--build-detailed-menu parse top info)
1321 (org-texinfo--build-menu parse 1 info 'detailed)))))
1323 ;;;; Node Property
1325 (defun org-texinfo-node-property (node-property contents info)
1326 "Transcode a NODE-PROPERTY element from Org to Texinfo.
1327 CONTENTS is nil. INFO is a plist holding contextual
1328 information."
1329 (format "%s:%s"
1330 (org-element-property :key node-property)
1331 (let ((value (org-element-property :value node-property)))
1332 (if value (concat " " value) ""))))
1334 ;;; Paragraph
1336 (defun org-texinfo-paragraph (paragraph contents info)
1337 "Transcode a PARAGRAPH element from Org to Texinfo.
1338 CONTENTS is the contents of the paragraph, as a string. INFO is
1339 the plist used as a communication channel."
1340 contents)
1342 ;;; Plain List
1344 (defun org-texinfo-plain-list (plain-list contents info)
1345 "Transcode a PLAIN-LIST element from Org to Texinfo.
1346 CONTENTS is the contents of the list. INFO is a plist holding
1347 contextual information."
1348 (let* ((attr (org-export-read-attribute :attr_texinfo plain-list))
1349 (indic (or (plist-get attr :indic)
1350 org-texinfo-def-table-markup))
1351 (type (org-element-property :type plain-list))
1352 (table-type (plist-get attr :table-type))
1353 ;; Ensure valid texinfo table type.
1354 (table-type (if (member table-type '("ftable" "vtable")) table-type
1355 "table"))
1356 (list-type (cond
1357 ((eq type 'ordered) "enumerate")
1358 ((eq type 'unordered) "itemize")
1359 ((eq type 'descriptive) table-type))))
1360 (format "@%s%s\n@end %s"
1361 (if (eq type 'descriptive)
1362 (concat list-type " " indic)
1363 list-type)
1364 contents
1365 list-type)))
1367 ;;; Plain Text
1369 (defun org-texinfo-plain-text (text info)
1370 "Transcode a TEXT string from Org to Texinfo.
1371 TEXT is the string to transcode. INFO is a plist holding
1372 contextual information."
1373 ;; First protect @, { and }.
1374 (let ((output (org-texinfo--sanitize-content text)))
1375 ;; Activate smart quotes. Be sure to provide original TEXT string
1376 ;; since OUTPUT may have been modified.
1377 (when (plist-get info :with-smart-quotes)
1378 (setq output
1379 (org-export-activate-smart-quotes output :texinfo info text)))
1380 ;; LaTeX into @LaTeX{} and TeX into @TeX{}
1381 (let ((case-fold-search nil)
1382 (start 0))
1383 (while (string-match "\\(\\(?:La\\)?TeX\\)" output start)
1384 (setq output (replace-match
1385 (format "@%s{}" (match-string 1 output)) nil t output)
1386 start (match-end 0))))
1387 ;; Convert special strings.
1388 (when (plist-get info :with-special-strings)
1389 (while (string-match (regexp-quote "...") output)
1390 (setq output (replace-match "@dots{}" nil t output))))
1391 ;; Handle break preservation if required.
1392 (when (plist-get info :preserve-breaks)
1393 (setq output (replace-regexp-in-string
1394 "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" output)))
1395 ;; Return value.
1396 output))
1398 ;;; Planning
1400 (defun org-texinfo-planning (planning contents info)
1401 "Transcode a PLANNING element from Org to Texinfo.
1402 CONTENTS is nil. INFO is a plist holding contextual
1403 information."
1404 (concat
1405 "@noindent"
1406 (mapconcat
1407 'identity
1408 (delq nil
1409 (list
1410 (let ((closed (org-element-property :closed planning)))
1411 (when closed
1412 (concat
1413 (format "@strong{%s} " org-closed-string)
1414 (format org-texinfo-inactive-timestamp-format
1415 (org-translate-time
1416 (org-element-property :raw-value closed))))))
1417 (let ((deadline (org-element-property :deadline planning)))
1418 (when deadline
1419 (concat
1420 (format "@strong{%s} " org-deadline-string)
1421 (format org-texinfo-active-timestamp-format
1422 (org-translate-time
1423 (org-element-property :raw-value deadline))))))
1424 (let ((scheduled (org-element-property :scheduled planning)))
1425 (when scheduled
1426 (concat
1427 (format "@strong{%s} " org-scheduled-string)
1428 (format org-texinfo-active-timestamp-format
1429 (org-translate-time
1430 (org-element-property :raw-value scheduled))))))))
1431 " ")
1432 "@*"))
1434 ;;; Property Drawer
1436 (defun org-texinfo-property-drawer (property-drawer contents info)
1437 "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
1438 CONTENTS holds the contents of the drawer. INFO is a plist
1439 holding contextual information."
1440 (and (org-string-nw-p contents)
1441 (format "@verbatim\n%s@end verbatim" contents)))
1443 ;;; Quote Block
1445 (defun org-texinfo-quote-block (quote-block contents info)
1446 "Transcode a QUOTE-BLOCK element from Org to Texinfo.
1447 CONTENTS holds the contents of the block. INFO is a plist
1448 holding contextual information."
1449 (let* ((title (org-element-property :name quote-block))
1450 (start-quote (concat "@quotation"
1451 (if title
1452 (format " %s" title)))))
1453 (format "%s\n%s@end quotation" start-quote contents)))
1455 ;;; Radio Target
1457 (defun org-texinfo-radio-target (radio-target text info)
1458 "Transcode a RADIO-TARGET object from Org to Texinfo.
1459 TEXT is the text of the target. INFO is a plist holding
1460 contextual information."
1461 (format "@anchor{%s}%s"
1462 (org-export-solidify-link-text
1463 (org-element-property :value radio-target))
1464 text))
1466 ;;; Section
1468 (defun org-texinfo-section (section contents info)
1469 "Transcode a SECTION element from Org to Texinfo.
1470 CONTENTS holds the contents of the section. INFO is a plist
1471 holding contextual information."
1472 contents)
1474 ;;; Special Block
1476 (defun org-texinfo-special-block (special-block contents info)
1477 "Transcode a SPECIAL-BLOCK element from Org to Texinfo.
1478 CONTENTS holds the contents of the block. INFO is a plist used
1479 as a communication channel."
1480 contents)
1482 ;;; Src Block
1484 (defun org-texinfo-src-block (src-block contents info)
1485 "Transcode a SRC-BLOCK element from Org to Texinfo.
1486 CONTENTS holds the contents of the item. INFO is a plist holding
1487 contextual information."
1488 (let* ((lang (org-element-property :language src-block))
1489 (lisp-p (string-match-p "lisp" lang))
1490 (src-contents (org-texinfo--sanitize-content
1491 (org-export-format-code-default src-block info))))
1492 (cond
1493 ;; Case 1. Lisp Block
1494 (lisp-p
1495 (format "@lisp\n%s@end lisp"
1496 src-contents))
1497 ;; Case 2. Other blocks
1499 (format "@example\n%s@end example"
1500 src-contents)))))
1502 ;;; Statistics Cookie
1504 (defun org-texinfo-statistics-cookie (statistics-cookie contents info)
1505 "Transcode a STATISTICS-COOKIE object from Org to Texinfo.
1506 CONTENTS is nil. INFO is a plist holding contextual information."
1507 (org-element-property :value statistics-cookie))
1509 ;;; Subscript
1511 (defun org-texinfo-subscript (subscript contents info)
1512 "Transcode a SUBSCRIPT object from Org to Texinfo.
1513 CONTENTS is the contents of the object. INFO is a plist holding
1514 contextual information."
1515 (format "@math{_%s}" contents))
1517 ;;; Superscript
1519 (defun org-texinfo-superscript (superscript contents info)
1520 "Transcode a SUPERSCRIPT object from Org to Texinfo.
1521 CONTENTS is the contents of the object. INFO is a plist holding
1522 contextual information."
1523 (format "@math{^%s}" contents))
1525 ;;; Table
1527 ;; `org-texinfo-table' is the entry point for table transcoding. It
1528 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
1529 ;; delegates the job to either `org-texinfo-table--table.el-table' or
1530 ;; `org-texinfo-table--org-table' functions, depending of the type of
1531 ;; the table.
1533 ;; `org-texinfo-table--align-string' is a subroutine used to build
1534 ;; alignment string for Org tables.
1536 (defun org-texinfo-table (table contents info)
1537 "Transcode a TABLE element from Org to Texinfo.
1538 CONTENTS is the contents of the table. INFO is a plist holding
1539 contextual information."
1540 (cond
1541 ;; Case 1: verbatim table.
1542 ((or org-texinfo-tables-verbatim
1543 (let ((attr (mapconcat 'identity
1544 (org-element-property :attr_latex table)
1545 " ")))
1546 (and attr (string-match "\\<verbatim\\>" attr))))
1547 (format "@verbatim \n%s\n@end verbatim"
1548 ;; Re-create table, without affiliated keywords.
1549 (org-trim
1550 (org-element-interpret-data
1551 `(table nil ,@(org-element-contents table))))))
1552 ;; Case 2: table.el table. Convert it using appropriate tools.
1553 ((eq (org-element-property :type table) 'table.el)
1554 (org-texinfo-table--table.el-table table contents info))
1555 ;; Case 3: Standard table.
1556 (t (org-texinfo-table--org-table table contents info))))
1558 (defun org-texinfo-table-column-widths (table info)
1559 "Determine the largest table cell in each column to process alignment.
1561 TABLE is the table element to transcode. INFO is a plist used as
1562 a communication channel."
1563 (let* ((rows (org-element-map table 'table-row 'identity info))
1564 (collected (loop for row in rows collect
1565 (org-element-map row 'table-cell 'identity info)))
1566 (number-cells (length (car collected)))
1567 cells counts)
1568 (loop for row in collected do
1569 (push (mapcar (lambda (ref)
1570 (let* ((start (org-element-property :contents-begin ref))
1571 (end (org-element-property :contents-end ref))
1572 (length (- end start)))
1573 length)) row) cells))
1574 (setq cells (org-remove-if 'null cells))
1575 (push (loop for count from 0 to (- number-cells 1) collect
1576 (loop for item in cells collect
1577 (nth count item))) counts)
1578 (mapconcat (lambda (size)
1579 (make-string size ?a)) (mapcar (lambda (ref)
1580 (apply 'max `(,@ref))) (car counts))
1581 "} {")))
1583 (defun org-texinfo-table--org-table (table contents info)
1584 "Return appropriate Texinfo code for an Org table.
1586 TABLE is the table type element to transcode. CONTENTS is its
1587 contents, as a string. INFO is a plist used as a communication
1588 channel.
1590 This function assumes TABLE has `org' as its `:type' attribute."
1591 (let* ((attr (org-export-read-attribute :attr_texinfo table))
1592 (col-width (plist-get attr :columns))
1593 (columns (if col-width
1594 (format "@columnfractions %s"
1595 col-width)
1596 (format "{%s}"
1597 (org-texinfo-table-column-widths
1598 table info)))))
1599 ;; Prepare the final format string for the table.
1600 (cond
1601 ;; Longtable.
1602 ;; Others.
1603 (t (concat
1604 (format "@multitable %s\n%s@end multitable"
1605 columns
1606 contents))))))
1608 (defun org-texinfo-table--table.el-table (table contents info)
1609 "Returns nothing.
1611 Rather than return an invalid table, nothing is returned."
1612 'nil)
1614 ;;; Table Cell
1616 (defun org-texinfo-table-cell (table-cell contents info)
1617 "Transcode a TABLE-CELL element from Org to Texinfo.
1618 CONTENTS is the cell contents. INFO is a plist used as
1619 a communication channel."
1620 (concat (if (and contents
1621 org-texinfo-table-scientific-notation
1622 (string-match orgtbl-exp-regexp contents))
1623 ;; Use appropriate format string for scientific
1624 ;; notation.
1625 (format org-texinfo-table-scientific-notation
1626 (match-string 1 contents)
1627 (match-string 2 contents))
1628 contents)
1629 (when (org-export-get-next-element table-cell info) "\n@tab ")))
1631 ;;; Table Row
1633 (defun org-texinfo-table-row (table-row contents info)
1634 "Transcode a TABLE-ROW element from Org to Texinfo.
1635 CONTENTS is the contents of the row. INFO is a plist used as
1636 a communication channel."
1637 ;; Rules are ignored since table separators are deduced from
1638 ;; borders of the current row.
1639 (when (eq (org-element-property :type table-row) 'standard)
1640 (let ((rowgroup-tag
1641 (cond
1642 ;; Case 1: Belongs to second or subsequent rowgroup.
1643 ((not (= 1 (org-export-table-row-group table-row info)))
1644 "@item ")
1645 ;; Case 2: Row is from first rowgroup. Table has >=1 rowgroups.
1646 ((org-export-table-has-header-p
1647 (org-export-get-parent-table table-row) info)
1648 "@headitem ")
1649 ;; Case 3: Row is from first and only row group.
1650 (t "@item "))))
1651 (when (eq (org-element-property :type table-row) 'standard)
1652 (concat rowgroup-tag contents "\n")))))
1654 ;;; Target
1656 (defun org-texinfo-target (target contents info)
1657 "Transcode a TARGET object from Org to Texinfo.
1658 CONTENTS is nil. INFO is a plist holding contextual
1659 information."
1660 (format "@anchor{%s}"
1661 (org-export-solidify-link-text (org-element-property :value target))))
1663 ;;; Timestamp
1665 (defun org-texinfo-timestamp (timestamp contents info)
1666 "Transcode a TIMESTAMP object from Org to Texinfo.
1667 CONTENTS is nil. INFO is a plist holding contextual
1668 information."
1669 (let ((value (org-texinfo-plain-text
1670 (org-timestamp-translate timestamp) info)))
1671 (case (org-element-property :type timestamp)
1672 ((active active-range)
1673 (format org-texinfo-active-timestamp-format value))
1674 ((inactive inactive-range)
1675 (format org-texinfo-inactive-timestamp-format value))
1676 (t (format org-texinfo-diary-timestamp-format value)))))
1678 ;;; Verbatim
1680 (defun org-texinfo-verbatim (verbatim contents info)
1681 "Transcode a VERBATIM object from Org to Texinfo.
1682 CONTENTS is nil. INFO is a plist used as a communication
1683 channel."
1684 (org-texinfo--text-markup (org-element-property :value verbatim) 'verbatim))
1686 ;;; Verse Block
1688 (defun org-texinfo-verse-block (verse-block contents info)
1689 "Transcode a VERSE-BLOCK element from Org to Texinfo.
1690 CONTENTS is verse block contents. INFO is a plist holding
1691 contextual information."
1692 ;; In a verse environment, add a line break to each newline
1693 ;; character and change each white space at beginning of a line
1694 ;; into a space of 1 em. Also change each blank line with
1695 ;; a vertical space of 1 em.
1696 (progn
1697 (setq contents (replace-regexp-in-string
1698 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
1699 (replace-regexp-in-string
1700 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
1701 (while (string-match "^[ \t]+" contents)
1702 (let ((new-str (format "\\hspace*{%dem}"
1703 (length (match-string 0 contents)))))
1704 (setq contents (replace-match new-str nil t contents))))
1705 (format "\\begin{verse}\n%s\\end{verse}" contents)))
1708 ;;; Interactive functions
1710 (defun org-texinfo-export-to-texinfo
1711 (&optional async subtreep visible-only body-only ext-plist)
1712 "Export current buffer to a Texinfo file.
1714 If narrowing is active in the current buffer, only export its
1715 narrowed part.
1717 If a region is active, export that region.
1719 A non-nil optional argument ASYNC means the process should happen
1720 asynchronously. The resulting file should be accessible through
1721 the `org-export-stack' interface.
1723 When optional argument SUBTREEP is non-nil, export the sub-tree
1724 at point, extracting information from the headline properties
1725 first.
1727 When optional argument VISIBLE-ONLY is non-nil, don't export
1728 contents of hidden elements.
1730 When optional argument BODY-ONLY is non-nil, only write code
1731 between \"\\begin{document}\" and \"\\end{document}\".
1733 EXT-PLIST, when provided, is a property list with external
1734 parameters overriding Org default settings, but still inferior to
1735 file-local settings.
1737 Return output file's name."
1738 (interactive)
1739 (let ((outfile (org-export-output-file-name ".texi" subtreep))
1740 (org-export-coding-system org-texinfo-coding-system))
1741 (org-export-to-file 'texinfo outfile
1742 async subtreep visible-only body-only ext-plist)))
1744 (defun org-texinfo-export-to-info
1745 (&optional async subtreep visible-only body-only ext-plist)
1746 "Export current buffer to Texinfo then process through to INFO.
1748 If narrowing is active in the current buffer, only export its
1749 narrowed part.
1751 If a region is active, export that region.
1753 A non-nil optional argument ASYNC means the process should happen
1754 asynchronously. The resulting file should be accessible through
1755 the `org-export-stack' interface.
1757 When optional argument SUBTREEP is non-nil, export the sub-tree
1758 at point, extracting information from the headline properties
1759 first.
1761 When optional argument VISIBLE-ONLY is non-nil, don't export
1762 contents of hidden elements.
1764 When optional argument BODY-ONLY is non-nil, only write code
1765 between \"\\begin{document}\" and \"\\end{document}\".
1767 EXT-PLIST, when provided, is a property list with external
1768 parameters overriding Org default settings, but still inferior to
1769 file-local settings.
1771 When optional argument PUB-DIR is set, use it as the publishing
1772 directory.
1774 Return INFO file's name."
1775 (interactive)
1776 (let ((outfile (org-export-output-file-name ".texi" subtreep))
1777 (org-export-coding-system org-texinfo-coding-system))
1778 (org-export-to-file 'texinfo outfile
1779 async subtreep visible-only body-only ext-plist
1780 (lambda (file) (org-texinfo-compile file)))))
1782 ;;;###autoload
1783 (defun org-texinfo-publish-to-texinfo (plist filename pub-dir)
1784 "Publish an org file to Texinfo.
1786 FILENAME is the filename of the Org file to be published. PLIST
1787 is the property list for the given project. PUB-DIR is the
1788 publishing directory.
1790 Return output file name."
1791 (org-publish-org-to 'texinfo filename ".texi" plist pub-dir))
1793 ;;;###autoload
1794 (defun org-texinfo-convert-region-to-texinfo ()
1795 "Assume the current region has org-mode syntax, and convert it to Texinfo.
1796 This can be used in any buffer. For example, you can write an
1797 itemized list in org-mode syntax in an Texinfo buffer and use
1798 this command to convert it."
1799 (interactive)
1800 (org-export-replace-region-by 'texinfo))
1802 (defun org-texinfo-compile (file)
1803 "Compile a texinfo file.
1805 FILE is the name of the file being compiled. Processing is
1806 done through the command specified in `org-texinfo-info-process'.
1808 Return INFO file name or an error if it couldn't be produced."
1809 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1810 (full-name (file-truename file))
1811 (out-dir (file-name-directory file))
1812 ;; Properly set working directory for compilation.
1813 (default-directory (if (file-name-absolute-p file)
1814 (file-name-directory full-name)
1815 default-directory))
1816 errors)
1817 (message (format "Processing Texinfo file %s..." file))
1818 (save-window-excursion
1819 (cond
1820 ;; A function is provided: Apply it.
1821 ((functionp org-texinfo-info-process)
1822 (funcall org-texinfo-info-process (shell-quote-argument file)))
1823 ;; A list is provided: Replace %b, %f and %o with appropriate
1824 ;; values in each command before applying it. Output is
1825 ;; redirected to "*Org INFO Texinfo Output*" buffer.
1826 ((consp org-texinfo-info-process)
1827 (let ((outbuf (get-buffer-create "*Org INFO Texinfo Output*")))
1828 (mapc
1829 (lambda (command)
1830 (shell-command
1831 (replace-regexp-in-string
1832 "%b" (shell-quote-argument base-name)
1833 (replace-regexp-in-string
1834 "%f" (shell-quote-argument full-name)
1835 (replace-regexp-in-string
1836 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1837 outbuf))
1838 org-texinfo-info-process)
1839 ;; Collect standard errors from output buffer.
1840 (setq errors (org-texinfo-collect-errors outbuf))))
1841 (t (error "No valid command to process to Info")))
1842 (let ((infofile (concat out-dir base-name ".info")))
1843 ;; Check for process failure. Provide collected errors if
1844 ;; possible.
1845 (if (not (file-exists-p infofile))
1846 (error (concat (format "INFO file %s wasn't produced" infofile)
1847 (when errors (concat ": " errors))))
1848 ;; Else remove log files, when specified, and signal end of
1849 ;; process to user, along with any error encountered.
1850 (when org-texinfo-remove-logfiles
1851 (dolist (ext org-texinfo-logfiles-extensions)
1852 (let ((file (concat out-dir base-name "." ext)))
1853 (when (file-exists-p file) (delete-file file)))))
1854 (message (concat "Process completed"
1855 (if (not errors) "."
1856 (concat " with errors: " errors)))))
1857 ;; Return output file name.
1858 infofile))))
1860 (defun org-texinfo-collect-errors (buffer)
1861 "Collect some kind of errors from \"makeinfo\" command output.
1863 BUFFER is the buffer containing output.
1865 Return collected error types as a string, or nil if there was
1866 none."
1867 (with-current-buffer buffer
1868 (save-excursion
1869 (goto-char (point-min))
1870 ;; Find final "makeinfo" run.
1871 (when t
1872 (let ((case-fold-search t)
1873 (errors ""))
1874 (when (save-excursion
1875 (re-search-forward "perhaps incorrect sectioning?" nil t))
1876 (setq errors (concat errors " [incorrect sectioning]")))
1877 (when (save-excursion
1878 (re-search-forward "missing close brace" nil t))
1879 (setq errors (concat errors " [syntax error]")))
1880 (when (save-excursion
1881 (re-search-forward "Unknown command" nil t))
1882 (setq errors (concat errors " [undefined @command]")))
1883 (when (save-excursion
1884 (re-search-forward "No matching @end" nil t))
1885 (setq errors (concat errors " [block incomplete]")))
1886 (when (save-excursion
1887 (re-search-forward "requires a sectioning" nil t))
1888 (setq errors (concat errors " [invalid section command]")))
1889 (when (save-excursion
1890 (re-search-forward "\\[unexpected\]" nil t))
1891 (setq errors (concat errors " [unexpected error]")))
1892 (when (save-excursion
1893 (re-search-forward "misplaced " nil t))
1894 (setq errors (concat errors " [syntax error]")))
1895 (and (org-string-nw-p errors) (org-trim errors)))))))
1898 (provide 'ox-texinfo)
1900 ;; Local variables:
1901 ;; generated-autoload-file: "org-loaddefs.el"
1902 ;; End:
1904 ;;; ox-texinfo.el ends here