1 ;;; org-e-beamer.el --- Beamer Back-End for Org Export Engine
3 ;; Copyright (C) 2007-2013 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten.dominik AT gmail DOT com>
6 ;; Nicolas Goaziou <n.goaziou AT gmail DOT com>
7 ;; Keywords: org, wp, tex
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/>.
24 ;; This library implements both a Beamer back-end, derived from the
25 ;; LaTeX one and a minor mode easing structure edition of the
28 ;; Depending on the desired output format, three commands are provided
29 ;; for export: `org-e-beamer-export-as-latex' (temporary buffer),
30 ;; `org-e-beamer-export-to-latex' ("tex" file) and
31 ;; `org-e-beamer-export-to-pdf' ("pdf" file).
33 ;; On top of buffer keywords and options items supported by `e-latex'
34 ;; back-end (see `org-e-latex-options-alist'), this back-end
35 ;; introduces the following keywords: "BEAMER_THEME",
36 ;; "BEAMER_COLOR_THEME", "BEAMER_FONT_THEME", "BEAMER_INNER_THEME" and
37 ;; "BEAMER_OUTER_THEME". All accept options in square brackets.
39 ;; Moreover, headlines now fall into three categories: sectioning
40 ;; elements, frames and blocks.
42 ;; - Like `e-latex' back-end sectioning elements are still set through
43 ;; `org-e-latex-classes' variable.
45 ;; - Headlines become frames when their level is equal to
46 ;; `org-e-beamer-frame-level' (or "H" value in the OPTIONS line).
47 ;; Though, if an headline in the current tree has a "BEAMER_env"
48 ;; (see below) property set to either "frame" or "fullframe", its
49 ;; level overrides the variable. A "fullframe" is a frame with an
50 ;; empty (ignored) title.
52 ;; - All frames' children become block environments. Special block
53 ;; types can be enforced by setting headline's "BEAMER_env" property
54 ;; to an appropriate value (see `org-e-beamer-environments-default'
55 ;; for supported value and `org-e-beamer-environments-extra' for
58 ;; - As a special case, if the "BEAMER_env" property is set to either
59 ;; "appendix", "note", "noteNH" or "againframe", the headline will
60 ;; become, respectively, an appendix, a note (within frame or
61 ;; between frame, depending on its level), a note with its title
62 ;; ignored or an againframe command. In the latter case,
63 ;; a "BEAMER_ref" property is mandatory in order to refer to the
64 ;; frame being resumed, and contents are ignored.
66 ;; Also, an headline with an "ignoreheading" value will have its
67 ;; contents only inserted in the output. This special value is
68 ;; useful to have data between frames, or to properly close
69 ;; a "column" environment.
71 ;; Along with "BEAMER_env", headlines also support "BEAMER_act" and
72 ;; "BEAMER_opt" properties. The former is translated as an
73 ;; overlay/action specification (or a default overlay specification
74 ;; when enclosed within square brackets) whereas the latter specifies
75 ;; options for the current frame ("fragile" option is added
76 ;; automatically, though).
78 ;; Every plain list has support for `:environment', `:overlay' and
79 ;; `:options' attributes (through ATTR_BEAMER affiliated keyword).
80 ;; The first one allows to use a different environment, the second
81 ;; sets overlay specifications and the last one inserts optional
82 ;; arguments in current list environment.
84 ;; Eventually, an export snippet with a value enclosed within angular
85 ;; brackets put at the beginning of an element or object whose type is
86 ;; among `bold', `item', `link', `radio-target' and `target' will
87 ;; control its overlay specifications.
89 ;; On the minor mode side, `org-e-beamer-select-environment' (bound by
90 ;; default to "C-c C-b") and `org-e-beamer-insert-options-template'
91 ;; are the two entry points.
95 (require 'org-e-latex
)
99 ;;; User-Configurable Variables
101 (defgroup org-export-e-beamer nil
102 "Options specific for using the beamer class in LaTeX export."
107 (defcustom org-e-beamer-frame-level
1
108 "The level at which headlines become frames.
110 Headlines at a lower level will be translated into a sectioning
111 structure. At a higher level, they will be translated into
114 If an headline with a \"BEAMER_env\" property set to \"frame\" is
115 found within a tree, its level locally overrides this number.
117 This variable has no effect on headlines with the \"BEAMER_env\"
118 property set to either \"ignoreheading\", \"appendix\", or
119 \"note\", which will respectively, be invisible, become an
122 This integer is relative to the minimal level of an headline
123 within the parse tree, defined as 1."
124 :group
'org-export-e-beamer
127 (defcustom org-e-beamer-frame-default-options
""
128 "Default options string to use for frames.
129 For example, it could be set to \"allowframebreaks\"."
130 :group
'org-export-e-beamer
131 :type
'(string :tag
"[options]"))
133 (defcustom org-e-beamer-column-view-format
134 "%45ITEM %10BEAMER_env(Env) %10BEAMER_act(Act) %4BEAMER_col(Col) %8BEAMER_opt(Opt)"
135 "Column view format that should be used to fill the template."
136 :group
'org-export-e-beamer
138 (const :tag
"Do not insert Beamer column view format" nil
)
139 (string :tag
"Beamer column view format")))
141 (defcustom org-e-beamer-theme
"default"
142 "Default theme used in Beamer presentations."
143 :group
'org-export-e-beamer
145 (const :tag
"Do not insert a Beamer theme" nil
)
146 (string :tag
"Beamer theme")))
148 (defcustom org-e-beamer-environments-extra nil
149 "Environments triggered by tags in Beamer export.
150 Each entry has 4 elements:
152 name Name of the environment
153 key Selection key for `org-e-beamer-select-environment'
154 open The opening template for the environment, with the following escapes
155 %a the action/overlay specification
156 %A the default action/overlay specification
157 %o the options argument of the template
159 %H if there is headline text, that text in {} braces
160 %U if there is headline text, that text in [] brackets
161 close The closing string of the environment."
162 :group
'org-export-e-beamer
165 (string :tag
"Environment")
166 (string :tag
"Selection key")
167 (string :tag
"Begin")
168 (string :tag
"End"))))
170 (defcustom org-e-beamer-outline-frame-title
"Outline"
171 "Default title of a frame containing an outline."
172 :group
'org-export-e-beamer
173 :type
'(string :tag
"Outline frame title"))
175 (defcustom org-e-beamer-outline-frame-options
""
176 "Outline frame options appended after \\begin{frame}.
177 You might want to put e.g. \"allowframebreaks=0.9\" here."
178 :group
'org-export-e-beamer
179 :type
'(string :tag
"Outline frame options"))
183 ;;; Internal Variables
185 (defconst org-e-beamer-column-widths
186 "0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 0.0 :ETC"
187 "The column widths that should be installed as allowed property values.")
189 (defconst org-e-beamer-environments-special
196 ("ignoreheading" "i")
199 "Alist of environments treated in a special way by the back-end.
200 Keys are environment names, as strings, values are bindings used
201 in `org-e-beamer-select-environment'. Environments listed here,
202 along with their binding, are hard coded and cannot be modified
203 through `org-e-beamer-environments-extra' variable.")
205 (defconst org-e-beamer-environments-default
206 '(("block" "b" "\\begin{block}%a{%h}" "\\end{block}")
207 ("alertblock" "a" "\\begin{alertblock}%a{%h}" "\\end{alertblock}")
208 ("verse" "v" "\\begin{verse}%a %% %h" "\\end{verse}")
209 ("quotation" "q" "\\begin{quotation}%a %% %h" "\\end{quotation}")
210 ("quote" "Q" "\\begin{quote}%a %% %h" "\\end{quote}")
211 ("structureenv" "s" "\\begin{structureenv}%a %% %h" "\\end{structureenv}")
212 ("theorem" "t" "\\begin{theorem}%a%U" "\\end{theorem}")
213 ("definition" "d" "\\begin{definition}%a%U" "\\end{definition}")
214 ("example" "e" "\\begin{example}%a%U" "\\end{example}")
215 ("exampleblock" "E" "\\begin{exampleblock}%a{%h}" "\\end{exampleblock}")
216 ("proof" "p" "\\begin{proof}%a%U" "\\end{proof}")
217 ("beamercolorbox" "o" "\\begin{beamercolorbox}%o{%h}" "\\end{beamercolorbox}"))
218 "Environments triggered by properties in Beamer export.
219 These are the defaults - for user definitions, see
220 `org-e-beamer-environments-extra'.")
222 (defconst org-e-beamer-verbatim-elements
223 '(code example-block fixed-width inline-src-block src-block verbatim
)
224 "List of element or object types producing verbatim text.
225 This is used internally to determine when a frame should have the
226 \"fragile\" option.")
230 ;;; Internal functions
232 (defun org-e-beamer--normalize-argument (argument type
)
233 "Return ARGUMENT string with proper boundaries.
235 TYPE is a symbol among the following:
236 `action' Return ARGUMENT within angular brackets.
237 `defaction' Return ARGUMENT within both square and angular brackets.
238 `option' Return ARGUMENT within square brackets."
239 (if (not (string-match "\\S-" argument
)) ""
241 (action (if (string-match "\\`<.*>\\'" argument
) argument
242 (format "<%s>" argument
)))
244 ((string-match "\\`\\[<.*>\\]\\'" argument
) argument
)
245 ((string-match "\\`<.*>\\'" argument
)
246 (format "[%s]" argument
))
247 ((string-match "\\`\\[\\(.*\\)\\]\\'" argument
)
248 (format "[<%s>]" (match-string 1 argument
)))
249 (t (format "[<%s>]" argument
))))
250 (option (if (string-match "\\`\\[.*\\]\\'" argument
) argument
251 (format "[%s]" argument
)))
252 (otherwise argument
))))
254 (defun org-e-beamer--element-has-overlay-p (element)
255 "Non-nil when ELEMENT has an overlay specified.
256 An element has an overlay specification when it starts with an
257 `e-beamer' export-snippet whose value is between angular
258 brackets. Return overlay specification, as a string, or nil."
259 (let ((first-object (car (org-element-contents element
))))
260 (when (eq (org-element-type first-object
) 'export-snippet
)
261 (let ((value (org-element-property :value first-object
)))
262 (and (string-match "\\`<.*>\\'" value
) value
)))))
268 (org-export-define-derived-backend e-beamer e-latex
269 :export-block
"BEAMER"
272 ((?B
"As TEX buffer (Beamer)" org-e-beamer-export-as-latex
)
273 (?b
"As TEX file (Beamer)" org-e-beamer-export-to-latex
)
274 (?P
"As PDF file (Beamer)" org-e-beamer-export-to-pdf
)
275 (?O
"As PDF file and open (Beamer)"
277 (if a
(org-e-beamer-export-to-pdf t s v b
)
278 (org-open-file (org-e-beamer-export-to-pdf nil s v b
)))))))
280 ((:beamer-theme
"BEAMER_THEME" nil org-e-beamer-theme
)
281 (:beamer-color-theme
"BEAMER_COLOR_THEME" nil nil t
)
282 (:beamer-font-theme
"BEAMER_FONT_THEME" nil nil t
)
283 (:beamer-inner-theme
"BEAMER_INNER_THEME" nil nil t
)
284 (:beamer-outer-theme
"BEAMER_OUTER_THEME" nil nil t
)
285 (:headline-levels nil
"H" org-e-beamer-frame-level
))
286 :translate-alist
((bold . org-e-beamer-bold
)
287 (export-block . org-e-beamer-export-block
)
288 (export-snippet . org-e-beamer-export-snippet
)
289 (headline . org-e-beamer-headline
)
290 (item . org-e-beamer-item
)
291 (keyword . org-e-beamer-keyword
)
292 (link . org-e-beamer-link
)
293 (plain-list . org-e-beamer-plain-list
)
294 (radio-target . org-e-beamer-radio-target
)
295 (target . org-e-beamer-target
)
296 (template . org-e-beamer-template
)))
300 ;;; Transcode Functions
304 (defun org-e-beamer-bold (bold contents info
)
305 "Transcode BLOCK object into Beamer code.
306 CONTENTS is the text being bold. INFO is a plist used as
307 a communication channel."
308 (format "\\alert%s{%s}"
309 (or (org-e-beamer--element-has-overlay-p bold
) "")
315 (defun org-e-beamer-export-block (export-block contents info
)
316 "Transcode an EXPORT-BLOCK element into Beamer code.
317 CONTENTS is nil. INFO is a plist used as a communication
319 (when (member (org-element-property :type export-block
) '("BEAMER" "LATEX"))
320 (org-remove-indentation (org-element-property :value export-block
))))
325 (defun org-e-beamer-export-snippet (export-snippet contents info
)
326 "Transcode an EXPORT-SNIPPET object into Beamer code.
327 CONTENTS is nil. INFO is a plist used as a communication
329 (let ((backend (org-export-snippet-backend export-snippet
))
330 (value (org-element-property :value export-snippet
)))
331 ;; Only "e-latex" and "e-beamer" snippets are retained.
332 (cond ((eq backend
'e-latex
) value
)
333 ;; Ignore "e-beamer" snippets specifying overlays.
334 ((and (eq backend
'e-beamer
)
335 (or (org-export-get-previous-element export-snippet info
)
336 (not (string-match "\\`<.*>\\'" value
))))
342 ;; The main function to translate an headline is
343 ;; `org-e-beamer-headline'.
345 ;; Depending on the level at which an headline is considered as
346 ;; a frame (given by `org-e-beamer--frame-level'), the headline is
347 ;; either a section (`org-e-beamer--format-section'), a frame
348 ;; (`org-e-beamer--format-frame') or a block
349 ;; (`org-e-beamer--format-block').
351 ;; `org-e-beamer-headline' also takes care of special environments
352 ;; like "ignoreheading", "note", "noteNH", "appendix" and
355 (defun org-e-beamer--get-label (headline info
)
356 "Return label for HEADLINE, as a string.
358 INFO is a plist used as a communication channel.
360 The value is either the label specified in \"BEAMER_opt\"
361 property, or a fallback value built from headline's number. This
362 function assumes HEADLINE will be treated as a frame."
363 (let ((opt (org-element-property :beamer-opt headline
)))
364 (if (and (org-string-nw-p opt
)
365 (string-match "\\(?:^\\|,\\)label=\\(.*?\\)\\(?:$\\|,\\)" opt
))
368 (mapconcat 'number-to-string
369 (org-export-get-headline-number headline info
)
372 (defun org-e-beamer--frame-level (headline info
)
373 "Return frame level in subtree containing HEADLINE.
374 INFO is a plist used as a communication channel."
376 ;; 1. Look for "frame" environment in parents, starting from the
379 (mapc (lambda (parent)
380 (let ((env (org-element-property :beamer-env parent
)))
381 (when (and env
(member (downcase env
) '("frame" "fullframe")))
382 (throw 'exit
(org-export-get-relative-level parent info
)))))
383 (nreverse (org-export-get-genealogy headline
)))
385 ;; 2. Look for "frame" environment in HEADLINE.
386 (let ((env (org-element-property :beamer-env headline
)))
387 (and env
(member (downcase env
) '("frame" "fullframe"))
388 (org-export-get-relative-level headline info
)))
389 ;; 3. Look for "frame" environment in sub-tree.
393 (let ((env (org-element-property :beamer-env hl
)))
394 (when (and env
(member (downcase env
) '("frame" "fullframe")))
395 (org-export-get-relative-level hl info
))))
397 ;; 4. No "frame" environment in tree: use default value.
398 (plist-get info
:headline-levels
)))
400 (defun org-e-beamer--format-section (headline contents info
)
401 "Format HEADLINE as a sectioning part.
402 CONTENTS holds the contents of the headline. INFO is a plist
403 used as a communication channel."
404 ;; Use `e-latex' back-end output, inserting overlay specifications
406 (let ((latex-headline (org-export-with-backend 'e-latex headline contents info
))
407 (mode-specs (org-element-property :beamer-act headline
)))
409 (string-match "\\`\\\\\\(.*?\\)\\(?:\\*\\|\\[.*\\]\\)?{"
411 (replace-match (concat (match-string 1 latex-headline
)
412 (format "<%s>" mode-specs
))
413 nil nil latex-headline
1)
416 (defun org-e-beamer--format-frame (headline contents info
)
417 "Format HEADLINE as a frame.
418 CONTENTS holds the contents of the headline. INFO is a plist
419 used as a communication channel."
421 ;; FRAGILEP is non-nil when HEADLINE contains an element
422 ;; among `org-e-beamer-verbatim-elements'.
423 (org-element-map headline org-e-beamer-verbatim-elements
'identity
425 (concat "\\begin{frame}"
426 ;; Overlay specification, if any. When surrounded by
427 ;; square brackets, consider it as a default
429 (let ((action (org-element-property :beamer-act headline
)))
432 ((string-match "\\`\\[.*\\]\\'" action
)
433 (org-e-beamer--normalize-argument action
'defaction
))
434 (t (org-e-beamer--normalize-argument action
'action
))))
436 (let* ((beamer-opt (org-element-property :beamer-opt headline
))
438 ;; Collect options from default value and headline's
439 ;; properties. Also add a label for links.
441 (org-split-string org-e-beamer-frame-default-options
",")
444 ;; Remove square brackets if user provided
446 (and (string-match "^\\[?\\(.*\\)\\]?$" beamer-opt
)
447 (match-string 1 beamer-opt
))
449 ;; Provide an automatic label for the frame
450 ;; unless the user specified one.
451 (unless (and beamer-opt
452 (string-match "\\(^\\|,\\)label=" beamer-opt
))
455 (org-e-beamer--get-label headline info
)))))))
456 ;; Change options list into a string.
457 (org-e-beamer--normalize-argument
460 (if (or (not fragilep
) (member "fragile" options
)) options
461 (cons "fragile" options
))
465 (let ((env (org-element-property :beamer-env headline
)))
467 (if (and env
(equal (downcase env
) "fullframe")) ""
469 (org-element-property :title headline
) info
))))
471 ;; The following workaround is required in fragile frames
472 ;; as Beamer will append "\par" to the beginning of the
473 ;; contents. So we need to make sure the command is
474 ;; separated from the contents by at least one space. If
475 ;; it isn't, it will create "\parfirst-word" command and
476 ;; remove the first word from the contents in the PDF
478 (if (not fragilep
) contents
479 (replace-regexp-in-string "\\`\n*" "\\& " contents
))
482 (defun org-e-beamer--format-block (headline contents info
)
483 "Format HEADLINE as a block.
484 CONTENTS holds the contents of the headline. INFO is a plist
485 used as a communication channel."
486 (let* ((column-width (org-element-property :beamer-col headline
))
487 ;; ENVIRONMENT defaults to "block" if none is specified and
488 ;; there is no column specification. If there is a column
489 ;; specified but still no explicit environment, ENVIRONMENT
491 (environment (let ((env (org-element-property :beamer-env headline
)))
493 ;; "block" is the fallback environment.
494 ((and (not env
) (not column-width
)) "block")
497 ;; Use specified environment.
498 (t (downcase env
)))))
499 (env-format (unless (member environment
'("column" "columns"))
501 (append org-e-beamer-environments-special
502 org-e-beamer-environments-extra
503 org-e-beamer-environments-default
))))
504 (title (org-export-data (org-element-property :title headline
) info
))
505 (options (let ((options (org-element-property :beamer-opt headline
)))
507 (org-e-beamer--normalize-argument options
'option
))))
508 ;; Start a "columns" environment when explicitly requested or
509 ;; when there is no previous headline or the previous
510 ;; headline do not have a BEAMER_column property.
511 (parent-env (org-element-property
512 :beamer-env
(org-export-get-parent-headline headline
)))
514 (or (equal environment
"columns")
517 (equal (downcase parent-env
) "columns")))
518 (or (org-export-first-sibling-p headline info
)
519 (not (org-element-property
521 (org-export-get-previous-element
523 ;; End the "columns" environment when explicitly requested or
524 ;; when there is no next headline or the next headline do not
525 ;; have a BEAMER_column property.
527 (or (equal environment
"columns")
530 (equal (downcase parent-env
) "columns")))
531 (or (org-export-last-sibling-p headline info
)
532 (not (org-element-property
534 (org-export-get-next-element headline info
))))))))
536 (when start-columns-p
537 ;; Column can accept options only when the environment is
538 ;; explicitly defined.
539 (if (not (equal environment
"columns")) "\\begin{columns}\n"
540 (format "\\begin{columns}%s\n" options
)))
542 (format "\\begin{column}%s{%s}\n"
543 ;; One can specify placement for column only when
544 ;; HEADLINE stands for a column on its own.
545 (if (equal environment
"column") options
"")
546 (format "%s\\textwidth" column-width
)))
547 ;; Block's opening string.
553 ;; If BEAMER_act property has its value enclosed in square
554 ;; brackets, it is a default overlay specification and
555 ;; overlay specification is empty. Otherwise, it is an
556 ;; overlay specification and the default one is nil.
557 (let ((action (org-element-property :beamer-act headline
)))
559 ((not action
) (list (cons "a" "") (cons "A" "")))
560 ((string-match "\\`\\[.*\\]\\'" action
)
562 (cons "A" (org-e-beamer--normalize-argument action
'defaction
))
565 (list (cons "a" (org-e-beamer--normalize-argument action
'action
))
567 (list (cons "o" options
)
569 (cons "H" (if (equal title
"") "" (format "{%s}" title
)))
570 (cons "U" (if (equal title
"") "" (format "[%s]" title
))))))
573 ;; Block's closing string.
574 (when environment
(concat (nth 3 env-format
) "\n"))
575 (when column-width
"\\end{column}\n")
576 (when end-columns-p
"\\end{columns}"))))
578 (defun org-e-beamer-headline (headline contents info
)
579 "Transcode HEADLINE element into Beamer code.
580 CONTENTS is the contents of the headline. INFO is a plist used
581 as a communication channel."
582 (unless (org-element-property :footnote-section-p headline
)
583 (let ((level (org-export-get-relative-level headline info
))
584 (frame-level (org-e-beamer--frame-level headline info
))
585 (environment (let ((env (org-element-property :beamer-env headline
)))
586 (if (stringp env
) (downcase env
) "block"))))
588 ;; Case 1: Resume frame specified by "BEAMER_ref" property.
589 ((equal environment
"againframe")
590 (let ((ref (org-element-property :beamer-ref headline
)))
591 ;; Reference to frame being resumed is mandatory. Ignore
592 ;; the whole headline if it isn't provided.
593 (when (org-string-nw-p ref
)
594 (concat "\\againframe"
595 ;; Overlay specification.
596 (let ((overlay (org-element-property :beamer-act headline
)))
598 (org-e-beamer--normalize-argument
600 (if (string-match "^\\[.*\\]$" overlay
) 'defaction
603 (let ((options (org-element-property :beamer-opt headline
)))
605 (org-e-beamer--normalize-argument options
'option
)))
606 ;; Resolve reference provided by "BEAMER_ref"
607 ;; property. This is done by building a minimal fake
608 ;; link and calling the appropriate resolve function,
609 ;; depending on the reference syntax.
612 (string-match "^\\(id:\\|#\\|\\*\\)?\\(.*\\)" ref
)
614 ((or (not (match-string 1 ref
))
615 (equal (match-string 1 ref
) "*")) 'fuzzy
)
616 ((equal (match-string 1 ref
) "id:") 'id
)
618 (link (list 'link
(list :path
(match-string 2 ref
))))
619 (target (if (eq type
'fuzzy
)
620 (org-export-resolve-fuzzy-link link info
)
621 (org-export-resolve-id-link link info
))))
622 ;; Now use user-defined label provided in TARGET
623 ;; headline, or fallback to standard one.
624 (format "{%s}" (org-e-beamer--get-label target info
)))))))
625 ;; Case 2: Creation of an appendix is requested.
626 ((equal environment
"appendix")
628 (org-element-property :beamer-act headline
)
630 (make-string (org-element-property :pre-blank headline
) ?
\n)
632 ;; Case 3: Ignore heading.
633 ((equal environment
"ignoreheading")
634 (concat (make-string (org-element-property :pre-blank headline
) ?
\n)
636 ;; Case 4: HEADLINE is a note.
637 ((member environment
'("note" "noteNH"))
639 (concat (and (equal environment
"note")
642 (org-element-property :title headline
) info
)
644 (org-trim contents
))))
645 ;; Case 5: HEADLINE is a frame.
646 ((= level frame-level
)
647 (org-e-beamer--format-frame headline contents info
))
648 ;; Case 6: Regular section, extracted from
649 ;; `org-e-latex-classes'.
650 ((< level frame-level
)
651 (org-e-beamer--format-section headline contents info
))
652 ;; Case 7: Otherwise, HEADLINE is a block.
653 (t (org-e-beamer--format-block headline contents info
))))))
658 (defun org-e-beamer-item (item contents info
)
659 "Transcode an ITEM element into Beamer code.
660 CONTENTS holds the contents of the item. INFO is a plist holding
661 contextual information."
662 (let ((action (let ((first-element (car (org-element-contents item
))))
663 (and (eq (org-element-type first-element
) 'paragraph
)
664 (org-e-beamer--element-has-overlay-p first-element
))))
665 (output (org-export-with-backend 'e-latex item contents info
)))
666 (if (not action
) output
667 ;; If the item starts with a paragraph and that paragraph starts
668 ;; with an export snippet specifying an overlay, insert it after
670 (replace-regexp-in-string "\\\\item" (concat "\\\\item" action
) output
))))
675 (defun org-e-beamer-keyword (keyword contents info
)
676 "Transcode a KEYWORD element into Beamer code.
677 CONTENTS is nil. INFO is a plist used as a communication
679 (let ((key (org-element-property :key keyword
))
680 (value (org-element-property :value keyword
)))
681 ;; Handle specifically BEAMER and TOC (headlines only) keywords.
682 ;; Otherwise, fallback to `e-latex' back-end.
684 ((equal key
"BEAMER") value
)
685 ((and (equal key
"TOC") (string-match "\\<headlines\\>" value
))
686 (let ((depth (or (and (string-match "[0-9]+" value
)
687 (string-to-number (match-string 0 value
)))
688 (plist-get info
:with-toc
)))
689 (options (and (string-match "\\[.*?\\]" value
)
690 (match-string 0 value
))))
693 (when (wholenump depth
) (format "\\setcounter{tocdepth}{%s}\n" depth
))
694 "\\tableofcontents" options
"\n"
696 (t (org-export-with-backend 'e-latex keyword contents info
)))))
701 (defun org-e-beamer-link (link contents info
)
702 "Transcode a LINK object into Beamer code.
703 CONTENTS is the description part of the link. INFO is a plist
704 used as a communication channel."
705 (let ((type (org-element-property :type link
))
706 (path (org-element-property :path link
)))
707 ;; Use \hyperlink command for all internal links.
709 ((equal type
"radio")
710 (let ((destination (org-export-resolve-radio-link link info
)))
712 (format "\\hyperlink%s{%s}{%s}"
713 (or (org-e-beamer--element-has-overlay-p link
) "")
714 (org-export-solidify-link-text path
)
715 (org-export-data (org-element-contents destination
) info
)))))
716 ((and (member type
'("custom-id" "fuzzy" "id"))
717 (let ((destination (if (string= type
"fuzzy")
718 (org-export-resolve-fuzzy-link link info
)
719 (org-export-resolve-id-link link info
))))
720 (case (org-element-type destination
)
726 (org-export-get-headline-number
729 (if (and (plist-get info
:section-numbers
) (not contents
))
730 (format "\\ref{%s}" label
)
731 (format "\\hyperlink%s{%s}{%s}"
732 (or (org-e-beamer--element-has-overlay-p link
) "")
736 (let ((path (org-export-solidify-link-text path
)))
737 (if (not contents
) (format "\\ref{%s}" path
)
738 (format "\\hyperlink%s{%s}{%s}"
739 (or (org-e-beamer--element-has-overlay-p link
) "")
742 ;; Otherwise, use `e-latex' back-end.
743 (t (org-export-with-backend 'e-latex link contents info
)))))
748 ;; Plain lists support `:environment', `:overlay' and `:options'
751 (defun org-e-beamer-plain-list (plain-list contents info
)
752 "Transcode a PLAIN-LIST element into Beamer code.
753 CONTENTS is the contents of the list. INFO is a plist holding
754 contextual information."
755 (let* ((type (org-element-property :type plain-list
))
756 (attributes (org-export-read-attribute :attr_beamer plain-list
))
757 (latex-type (let ((env (plist-get attributes
:environment
)))
758 (cond (env (format "%s" env
))
759 ((eq type
'ordered
) "enumerate")
760 ((eq type
'descriptive
) "description")
762 (org-e-latex--wrap-label
764 (format "\\begin{%s}%s%s\n%s\\end{%s}"
766 ;; Default overlay specification, if any.
767 (org-e-beamer--normalize-argument
768 (format "%s" (or (plist-get attributes
:overlay
) ""))
770 ;; Second optional argument depends on the list type.
771 (org-e-beamer--normalize-argument
772 (format "%s" (or (plist-get attributes
:options
) ""))
774 ;; Eventually insert contents and close environment.
781 (defun org-e-beamer-radio-target (radio-target text info
)
782 "Transcode a RADIO-TARGET object into Beamer code.
783 TEXT is the text of the target. INFO is a plist holding
784 contextual information."
785 (format "\\hypertarget%s{%s}{%s}"
786 (or (org-e-beamer--element-has-overlay-p radio-target
) "")
787 (org-export-solidify-link-text
788 (org-element-property :value radio-target
))
794 (defun org-e-beamer-target (target contents info
)
795 "Transcode a TARGET object into Beamer code.
796 CONTENTS is nil. INFO is a plist holding contextual
798 (format "\\hypertarget{%s}{}"
799 (org-export-solidify-link-text (org-element-property :value target
))))
804 ;; Template used is similar to the one used in `e-latex' back-end,
805 ;; excepted for the table of contents and Beamer themes.
807 (defun org-e-beamer-template (contents info
)
808 "Return complete document string after Beamer conversion.
809 CONTENTS is the transcoded contents string. INFO is a plist
810 holding export options."
811 (let ((title (org-export-data (plist-get info
:title
) info
)))
814 (and (plist-get info
:time-stamp-file
)
815 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
816 ;; 2. Document class and packages.
817 (let ((class (plist-get info
:latex-class
))
818 (class-options (plist-get info
:latex-class-options
)))
819 (org-element-normalize-string
820 (let* ((header (nth 1 (assoc class org-e-latex-classes
)))
821 (document-class-string
822 (and (stringp header
)
823 (if (not class-options
) header
824 (replace-regexp-in-string
825 "^[ \t]*\\\\documentclass\\(\\(\\[.*\\]\\)?\\)"
826 class-options header t nil
1)))))
827 (when document-class-string
828 (org-e-latex--guess-babel-language
829 (org-e-latex--guess-inputenc
830 (org-splice-latex-header
831 document-class-string
832 org-export-latex-default-packages-alist
; defined in org.el
833 org-export-latex-packages-alist nil
; defined in org.el
834 (plist-get info
:latex-header-extra
)))
839 (lambda (prop command
)
840 (let ((theme (plist-get info prop
)))
843 (if (not (string-match "\\[.*\\]" theme
))
844 (format "{%s}\n" theme
)
846 (match-string 0 theme
)
848 (replace-match "" nil nil theme
)))))))))))
849 (mapconcat (lambda (args) (apply format-theme args
))
850 '((:beamer-theme
"\\usetheme")
851 (:beamer-color-theme
"\\usecolortheme")
852 (:beamer-font-theme
"\\usefonttheme")
853 (:beamer-inner-theme
"\\useinnertheme")
854 (:beamer-outer-theme
"\\useoutertheme"))
856 ;; 4. Possibly limit depth for headline numbering.
857 (let ((sec-num (plist-get info
:section-numbers
)))
858 (when (integerp sec-num
)
859 (format "\\setcounter{secnumdepth}{%d}\n" sec-num
)))
861 (let ((author (and (plist-get info
:with-author
)
862 (let ((auth (plist-get info
:author
)))
863 (and auth
(org-export-data auth info
)))))
864 (email (and (plist-get info
:with-email
)
865 (org-export-data (plist-get info
:email
) info
))))
866 (cond ((and author email
(not (string= "" email
)))
867 (format "\\author{%s\\thanks{%s}}\n" author email
))
868 (author (format "\\author{%s}\n" author
))
871 (let ((date (and (plist-get info
:with-date
)
872 (org-export-data (plist-get info
:date
) info
))))
873 (format "\\date{%s}\n" (or date
"")))
875 (format "\\title{%s}\n" title
)
876 ;; 8. Hyperref options.
877 (when (plist-get info
:latex-hyperref-p
)
878 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
879 (or (plist-get info
:keywords
) "")
880 (or (plist-get info
:description
) "")
881 (if (not (plist-get info
:with-creator
)) ""
882 (plist-get info
:creator
))))
883 ;; 9. Document start.
884 "\\begin{document}\n\n"
885 ;; 10. Title command.
886 (org-element-normalize-string
887 (cond ((string= "" title
) nil
)
888 ((not (stringp org-e-latex-title-command
)) nil
)
889 ((string-match "\\(?:[^%]\\|^\\)%s"
890 org-e-latex-title-command
)
891 (format org-e-latex-title-command title
))
892 (t org-e-latex-title-command
)))
893 ;; 11. Table of contents.
894 (let ((depth (plist-get info
:with-toc
)))
897 (format "\\begin{frame}%s{%s}\n"
898 (org-e-beamer--normalize-argument
899 org-e-beamer-outline-frame-options
'option
)
900 org-e-beamer-outline-frame-title
)
901 (when (wholenump depth
)
902 (format "\\setcounter{tocdepth}{%d}\n" depth
))
903 "\\tableofcontents\n"
904 "\\end{frame}\n\n")))
905 ;; 12. Document's body.
908 (let ((creator-info (plist-get info
:with-creator
)))
910 ((not creator-info
) "")
911 ((eq creator-info
'comment
)
912 (format "%% %s\n" (plist-get info
:creator
)))
913 (t (concat (plist-get info
:creator
) "\n"))))
922 (defvar org-e-beamer-mode-map
(make-sparse-keymap)
923 "The keymap for `org-e-beamer-mode'.")
924 (define-key org-e-beamer-mode-map
"\C-c\C-b" 'org-e-beamer-select-environment
)
927 (define-minor-mode org-e-beamer-mode
928 "Support for editing Beamer oriented Org mode files."
929 nil
" Bm" 'org-e-beamer-mode-map
)
931 (when (fboundp 'font-lock-add-keywords
)
932 (font-lock-add-keywords
934 '((":\\(B_[a-z]+\\|BMCOL\\):" 1 'org-e-beamer-tag prepend
))
937 (defface org-e-beamer-tag
'((t (:box
(:line-width
1 :color grey40
))))
938 "The special face for beamer tags."
939 :group
'org-export-e-beamer
)
941 (defun org-e-beamer-property-changed (property value
)
942 "Track the BEAMER_env property with tags.
943 PROPERTY is the name of the modified property. VALUE is its new
946 ((equal property
"BEAMER_env")
948 (org-back-to-heading t
)
949 ;; Filter out Beamer-related tags and install environment tag.
950 (let ((tags (org-remove-if (lambda (x) (string-match "^B_" x
))
952 (env-tag (and (org-string-nw-p value
) (concat "B_" value
))))
953 (org-set-tags-to (if env-tag
(cons env-tag tags
) tags
))
954 (when env-tag
(org-toggle-tag env-tag
'on
)))))
955 ((equal property
"BEAMER_col")
956 (org-toggle-tag "BMCOL" (if (org-string-nw-p value
) 'on
'off
)))))
958 (add-hook 'org-property-changed-functions
'org-e-beamer-property-changed
)
960 (defun org-e-beamer-allowed-property-values (property)
961 "Supply allowed values for PROPERTY."
963 ((and (equal property
"BEAMER_env")
964 (not (org-entry-get nil
(concat property
"_ALL") 'inherit
)))
965 ;; If no allowed values for BEAMER_env have been defined,
966 ;; supply all defined environments
967 (mapcar 'car
(append org-e-beamer-environments-special
968 org-e-beamer-environments-extra
969 org-e-beamer-environments-default
)))
970 ((and (equal property
"BEAMER_col")
971 (not (org-entry-get nil
(concat property
"_ALL") 'inherit
)))
972 ;; If no allowed values for BEAMER_col have been defined,
974 (org-split-string org-e-beamer-column-widths
" "))))
976 (add-hook 'org-property-allowed-value-functions
977 'org-e-beamer-allowed-property-values
)
984 (defun org-e-beamer-export-as-latex
985 (&optional async subtreep visible-only body-only ext-plist
)
986 "Export current buffer as a Beamer buffer.
988 If narrowing is active in the current buffer, only export its
991 If a region is active, export that region.
993 A non-nil optional argument ASYNC means the process should happen
994 asynchronously. The resulting buffer should be accessible
995 through the `org-export-stack' interface.
997 When optional argument SUBTREEP is non-nil, export the sub-tree
998 at point, extracting information from the headline properties
1001 When optional argument VISIBLE-ONLY is non-nil, don't export
1002 contents of hidden elements.
1004 When optional argument BODY-ONLY is non-nil, only write code
1005 between \"\\begin{document}\" and \"\\end{document}\".
1007 EXT-PLIST, when provided, is a property list with external
1008 parameters overriding Org default settings, but still inferior to
1009 file-local settings.
1011 Export is done in a buffer named \"*Org E-BEAMER Export*\", which
1012 will be displayed when `org-export-show-temporary-export-buffer'
1016 (org-export-async-start
1018 (with-current-buffer (get-buffer-create "*Org E-BEAMER Export*")
1021 (goto-char (point-min))
1023 (org-export-add-to-stack (current-buffer) 'e-beamer
)))
1024 `(org-export-as 'e-beamer
,subtreep
,visible-only
,body-only
1026 (let ((outbuf (org-export-to-buffer
1027 'e-beamer
"*Org E-BEAMER Export*"
1028 subtreep visible-only body-only ext-plist
)))
1029 (with-current-buffer outbuf
(LaTeX-mode))
1030 (when org-export-show-temporary-export-buffer
1031 (switch-to-buffer-other-window outbuf
)))))
1034 (defun org-e-beamer-export-to-latex
1035 (&optional async subtreep visible-only body-only ext-plist
)
1036 "Export current buffer as a Beamer presentation (tex).
1038 If narrowing is active in the current buffer, only export its
1041 If a region is active, export that region.
1043 A non-nil optional argument ASYNC means the process should happen
1044 asynchronously. The resulting file should be accessible through
1045 the `org-export-stack' interface.
1047 When optional argument SUBTREEP is non-nil, export the sub-tree
1048 at point, extracting information from the headline properties
1051 When optional argument VISIBLE-ONLY is non-nil, don't export
1052 contents of hidden elements.
1054 When optional argument BODY-ONLY is non-nil, only write code
1055 between \"\\begin{document}\" and \"\\end{document}\".
1057 EXT-PLIST, when provided, is a property list with external
1058 parameters overriding Org default settings, but still inferior to
1059 file-local settings.
1061 Return output file's name."
1063 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
1065 (org-export-async-start
1066 (lambda (f) (org-export-add-to-stack f
'e-beamer
))
1069 'e-beamer
,outfile
,subtreep
,visible-only
,body-only
1072 'e-beamer outfile subtreep visible-only body-only ext-plist
))))
1075 (defun org-e-beamer-export-to-pdf
1076 (&optional async subtreep visible-only body-only ext-plist
)
1077 "Export current buffer as a Beamer presentation (PDF).
1079 If narrowing is active in the current buffer, only export its
1082 If a region is active, export that region.
1084 A non-nil optional argument ASYNC means the process should happen
1085 asynchronously. The resulting file should be accessible through
1086 the `org-export-stack' interface.
1088 When optional argument SUBTREEP is non-nil, export the sub-tree
1089 at point, extracting information from the headline properties
1092 When optional argument VISIBLE-ONLY is non-nil, don't export
1093 contents of hidden elements.
1095 When optional argument BODY-ONLY is non-nil, only write code
1096 between \"\\begin{document}\" and \"\\end{document}\".
1098 EXT-PLIST, when provided, is a property list with external
1099 parameters overriding Org default settings, but still inferior to
1100 file-local settings.
1102 Return PDF file's name."
1105 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
1106 (org-export-async-start
1107 (lambda (f) (org-export-add-to-stack f
'e-beamer
))
1109 (org-e-latex-compile
1111 'e-beamer
,outfile
,subtreep
,visible-only
,body-only
1113 (org-e-latex-compile
1114 (org-e-beamer-export-to-latex
1115 nil subtreep visible-only body-only ext-plist
))))
1118 (defun org-e-beamer-select-environment ()
1119 "Select the environment to be used by beamer for this entry.
1120 While this uses (for convenience) a tag selection interface, the
1121 result of this command will be that the BEAMER_env *property* of
1124 In addition to this, the command will also set a tag as a visual
1125 aid, but the tag does not have any semantic meaning."
1127 ;; Make sure `org-e-beamer-environments-special' has a higher
1128 ;; priority than `org-e-beamer-environments-extra'.
1129 (let* ((envs (append org-e-beamer-environments-special
1130 org-e-beamer-environments-extra
1131 org-e-beamer-environments-default
))
1133 (append '((:startgroup
))
1134 (mapcar (lambda (e) (cons (concat "B_" (car e
))
1135 (string-to-char (nth 1 e
))))
1139 (org-fast-tag-selection-single-key t
))
1141 (let ((tags (or (ignore-errors (org-get-tags-string)) "")))
1143 ;; For a column, automatically ask for its width.
1144 ((eq org-last-tag-selection-key ?|
)
1145 (if (string-match ":BMCOL:" tags
)
1146 (org-set-property "BEAMER_col" (read-string "Column width: "))
1147 (org-delete-property "BEAMER_col")))
1148 ;; For an "againframe" section, automatically ask for reference
1149 ;; to resumed frame and overlay specifications.
1150 ((eq org-last-tag-selection-key ?A
)
1151 (if (equal (org-entry-get nil
"BEAMER_env") "againframe")
1152 (progn (org-entry-delete nil
"BEAMER_env")
1153 (org-entry-delete nil
"BEAMER_ref")
1154 (org-entry-delete nil
"BEAMER_act"))
1155 (org-entry-put nil
"BEAMER_env" "againframe")
1158 (read-string "Frame reference (*Title, #custom-id, id:...): "))
1159 (org-set-property "BEAMER_act"
1160 (read-string "Overlay specification: "))))
1161 ((string-match (concat ":B_\\(" (mapconcat 'car envs
"\\|") "\\):") tags
)
1162 (org-entry-put nil
"BEAMER_env" (match-string 1 tags
)))
1163 (t (org-entry-delete nil
"BEAMER_env"))))))
1166 (defun org-e-beamer-insert-options-template (&optional kind
)
1167 "Insert a settings template, to make sure users do this right."
1169 (message "Current [s]ubtree or [g]lobal?")
1170 (if (eq (read-char-exclusive) ?g
) (list 'global
)
1172 (if (eq kind
'subtree
)
1174 (org-back-to-heading t
)
1176 (org-entry-put nil
"EXPORT_LaTeX_CLASS" "beamer")
1177 (org-entry-put nil
"EXPORT_LaTeX_CLASS_OPTIONS" "[presentation]")
1178 (org-entry-put nil
"EXPORT_FILE_NAME" "presentation.pdf")
1179 (when org-e-beamer-column-view-format
1180 (org-entry-put nil
"COLUMNS" org-e-beamer-column-view-format
))
1181 (org-entry-put nil
"BEAMER_col_ALL" org-e-beamer-column-widths
))
1182 (insert "#+LaTeX_CLASS: beamer\n")
1183 (insert "#+LaTeX_CLASS_OPTIONS: [presentation]\n")
1184 (when org-e-beamer-theme
1185 (insert "#+BEAMER_THEME: " org-e-beamer-theme
"\n"))
1186 (when org-e-beamer-column-view-format
1187 (insert "#+COLUMNS: " org-e-beamer-column-view-format
"\n"))
1188 (insert "#+PROPERTY: BEAMER_col_ALL " org-e-beamer-column-widths
"\n")))
1191 (defun org-e-beamer-publish-to-latex (plist filename pub-dir
)
1192 "Publish an Org file to a Beamer presentation (LaTeX).
1194 FILENAME is the filename of the Org file to be published. PLIST
1195 is the property list for the given project. PUB-DIR is the
1196 publishing directory.
1198 Return output file name."
1199 (org-e-publish-org-to 'e-beamer filename
".tex" plist pub-dir
))
1202 (defun org-e-beamer-publish-to-pdf (plist filename pub-dir
)
1203 "Publish an Org file to a Beamer presentation (PDF, via LaTeX).
1205 FILENAME is the filename of the Org file to be published. PLIST
1206 is the property list for the given project. PUB-DIR is the
1207 publishing directory.
1209 Return output file name."
1210 ;; Unlike to `org-e-beamer-publish-to-latex', PDF file is generated
1211 ;; in working directory and then moved to publishing directory.
1212 (org-e-publish-attachment
1214 (org-e-latex-compile (org-e-publish-org-to 'e-beamer filename
".tex" plist
))
1218 (provide 'org-e-beamer
)
1219 ;;; org-e-beamer.el ends here