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