f3035362a578f19c7f9b3470003dbadf0b409697
[org-mode.git] / contrib / lisp / org-e-beamer.el
blobf3035362a578f19c7f9b3470003dbadf0b409697
1 ;;; org-e-beamer.el --- Beamer Back-End for Org Export Engine
3 ;; Copyright (C) 2007-2012 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/>.
22 ;;; Commentary:
24 ;; This library implements both a Beamer back-end, derived from the
25 ;; LaTeX one and a minor mode easing structure edition of the
26 ;; document.
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 supported by `e-latex' back-end (see
34 ;; `org-e-latex-options-alist'), this back-end introduces the
35 ;; following keywords: "BEAMER_THEME", "BEAMER_COLOR_THEME",
36 ;; "BEAMER_FONT_THEME", "BEAMER_INNER_THEME" and "BEAMER_OUTER_THEME".
37 ;; 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
56 ;; adding more).
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 `:overlay' attribute (through
79 ;; ATTR_BEAMER affiliated keyword). Also, ordered (resp. description)
80 ;; lists make use of `:template' (resp. `:long-text') attribute.
82 ;; Eventually, an export snippet with a value enclosed within angular
83 ;; brackets put at the beginning of an element or object whose type is
84 ;; among `bold', `item', `link', `radio-target' and `target' will
85 ;; control its overlay specifications.
87 ;; On the minor mode side, `org-e-beamer-select-environment' (bound by
88 ;; default to "C-c C-b") and `org-e-beamer-insert-options-template'
89 ;; are the two entry points.
91 ;;; Code:
93 (require 'org-e-latex)
97 ;;; User-Configurable Variables
99 (defgroup org-export-e-beamer nil
100 "Options specific for using the beamer class in LaTeX export."
101 :tag "Org Beamer"
102 :group 'org-export
103 :version "24.2")
105 (defcustom org-e-beamer-frame-level 1
106 "The level at which headlines become frames.
108 Headlines at a lower level will be translated into a sectioning
109 structure. At a higher level, they will be translated into
110 blocks.
112 If an headline with a \"BEAMER_env\" property set to \"frame\" is
113 found within a tree, its level locally overrides this number.
115 This variable has no effect on headlines with the \"BEAMER_env\"
116 property set to either \"ignoreheading\", \"appendix\", or
117 \"note\", which will respectively, be invisible, become an
118 appendix or a note.
120 This integer is relative to the minimal level of an headline
121 within the parse tree, defined as 1."
122 :group 'org-export-e-beamer
123 :type 'integer)
125 (defcustom org-e-beamer-frame-default-options ""
126 "Default options string to use for frames.
127 For example, it could be set to \"allowframebreaks\"."
128 :group 'org-export-e-beamer
129 :type '(string :tag "[options]"))
131 (defcustom org-e-beamer-column-view-format
132 "%45ITEM %10BEAMER_env(Env) %10BEAMER_act(Act) %4BEAMER_col(Col) %8BEAMER_opt(Opt)"
133 "Column view format that should be used to fill the template."
134 :group 'org-export-e-beamer
135 :type '(choice
136 (const :tag "Do not insert Beamer column view format" nil)
137 (string :tag "Beamer column view format")))
139 (defcustom org-e-beamer-theme "default"
140 "Default theme used in Beamer presentations."
141 :group 'org-export-e-beamer
142 :type '(choice
143 (const :tag "Do not insert a Beamer theme" nil)
144 (string :tag "Beamer theme")))
146 (defcustom org-e-beamer-environments-extra nil
147 "Environments triggered by tags in Beamer export.
148 Each entry has 4 elements:
150 name Name of the environment
151 key Selection key for `org-e-beamer-select-environment'
152 open The opening template for the environment, with the following escapes
153 %a the action/overlay specification
154 %A the default action/overlay specification
155 %o the options argument of the template
156 %h the headline text
157 %H if there is headline text, that text in {} braces
158 %U if there is headline text, that text in [] brackets
159 close The closing string of the environment."
160 :group 'org-export-e-beamer
161 :type '(repeat
162 (list
163 (string :tag "Environment")
164 (string :tag "Selection key")
165 (string :tag "Begin")
166 (string :tag "End"))))
168 (defcustom org-e-beamer-outline-frame-title "Outline"
169 "Default title of a frame containing an outline."
170 :group 'org-export-e-beamer
171 :type '(string :tag "Outline frame title"))
173 (defcustom org-e-beamer-outline-frame-options ""
174 "Outline frame options appended after \\begin{frame}.
175 You might want to put e.g. \"allowframebreaks=0.9\" here."
176 :group 'org-export-e-beamer
177 :type '(string :tag "Outline frame options"))
181 ;;; Internal Variables
183 (defconst org-e-beamer-column-widths
184 "0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 0.0 :ETC"
185 "The column widths that should be installed as allowed property values.")
187 (defconst org-e-beamer-environments-special
188 '(("againframe" "A")
189 ("appendix" "x")
190 ("column" "c")
191 ("columns" "C")
192 ("frame" "f")
193 ("fullframe" "F")
194 ("ignoreheading" "i")
195 ("note" "n")
196 ("noteNH" "N"))
197 "Alist of environments treated in a special way by the back-end.
198 Keys are environment names, as strings, values are bindings used
199 in `org-e-beamer-select-environment'. Environments listed here,
200 along with their binding, are hard coded and cannot be modified
201 through `org-e-beamer-environments-extra' variable.")
203 (defconst org-e-beamer-environments-default
204 '(("block" "b" "\\begin{block}%a{%h}" "\\end{block}")
205 ("alertblock" "a" "\\begin{alertblock}%a{%h}" "\\end{alertblock}")
206 ("verse" "v" "\\begin{verse}%a %% %h" "\\end{verse}")
207 ("quotation" "q" "\\begin{quotation}%a %% %h" "\\end{quotation}")
208 ("quote" "Q" "\\begin{quote}%a %% %h" "\\end{quote}")
209 ("structureenv" "s" "\\begin{structureenv}%a %% %h" "\\end{structureenv}")
210 ("theorem" "t" "\\begin{theorem}%a%U" "\\end{theorem}")
211 ("definition" "d" "\\begin{definition}%a%U" "\\end{definition}")
212 ("example" "e" "\\begin{example}%a%U" "\\end{example}")
213 ("exampleblock" "E" "\\begin{exampleblock}%a{%h}" "\\end{exampleblock}")
214 ("proof" "p" "\\begin{proof}%a%U" "\\end{proof}")
215 ("beamercolorbox" "o" "\\begin{beamercolorbox}%o{%h}" "\\end{beamercolorbox}"))
216 "Environments triggered by properties in Beamer export.
217 These are the defaults - for user definitions, see
218 `org-e-beamer-environments-extra'.")
220 (defconst org-e-beamer-verbatim-elements
221 '(code example-block fixed-width inline-src-block src-block verbatim)
222 "List of element or object types producing verbatim text.
223 This is used internally to determine when a frame should have the
224 \"fragile\" option.")
228 ;;; Internal functions
230 (defun org-e-beamer--normalize-argument (argument type)
231 "Return ARGUMENT string with proper boundaries.
233 TYPE is a symbol among the following:
234 `action' Return ARGUMENT within angular brackets.
235 `defaction' Return ARGUMENT within both square and angular brackets.
236 `option' Return ARGUMENT within square brackets."
237 (if (not (string-match "\\S-" argument)) ""
238 (case type
239 (action (if (string-match "\\`<.*>\\'" argument) argument
240 (format "<%s>" argument)))
241 (defaction (cond
242 ((string-match "\\`\\[<.*>\\]\\'" argument) argument)
243 ((string-match "\\`<.*>\\'" argument)
244 (format "[%s]" argument))
245 ((string-match "\\`\\[\\(.*\\)\\]\\'" argument)
246 (format "[<%s>]" (match-string 1 argument)))
247 (t (format "[<%s>]" argument))))
248 (option (if (string-match "\\`\\[.*\\]\\'" argument) argument
249 (format "[%s]" argument)))
250 (otherwise argument))))
252 (defun org-e-beamer--element-has-overlay-p (element)
253 "Non-nil when ELEMENT has an overlay specified.
254 An element has an overlay specification when it starts with an
255 `e-beamer' export-snippet whose value is between angular
256 brackets. Return overlay specification, as a string, or nil."
257 (let ((first-object (car (org-element-contents element))))
258 (when (eq (org-element-type first-object) 'export-snippet)
259 (let ((value (org-element-property :value first-object)))
260 (and (string-match "\\`<.*>\\'" value) value)))))
264 ;;; Define Back-End
266 (org-export-define-derived-backend e-beamer e-latex
267 :export-block "BEAMER"
268 :menu-entry
269 (?l 1
270 ((?B "As TEX buffer (Beamer)" org-e-beamer-export-as-latex)
271 (?b "As TEX file (Beamer)" org-e-beamer-export-to-latex)
272 (?P "As PDF file (Beamer)" org-e-beamer-export-to-pdf)
273 (?O "As PDF file and open (Beamer)"
274 (lambda (s v b)
275 (org-open-file (org-e-beamer-export-to-pdf s v b))))))
276 :options-alist
277 ((:beamer-theme "BEAMER_THEME" nil org-e-beamer-theme)
278 (:beamer-color-theme "BEAMER_COLOR_THEME" nil nil t)
279 (:beamer-font-theme "BEAMER_FONT_THEME" nil nil t)
280 (:beamer-inner-theme "BEAMER_INNER_THEME" nil nil t)
281 (:beamer-outer-theme "BEAMER_OUTER_THEME" nil nil t)
282 (:headline-levels nil "H" org-e-beamer-frame-level))
283 :translate-alist ((bold . org-e-beamer-bold)
284 (export-block . org-e-beamer-export-block)
285 (export-snippet . org-e-beamer-export-snippet)
286 (headline . org-e-beamer-headline)
287 (item . org-e-beamer-item)
288 (keyword . org-e-beamer-keyword)
289 (link . org-e-beamer-link)
290 (plain-list . org-e-beamer-plain-list)
291 (radio-target . org-e-beamer-radio-target)
292 (target . org-e-beamer-target)
293 (template . org-e-beamer-template)))
297 ;;; Transcode Functions
299 ;;;; Bold
301 (defun org-e-beamer-bold (bold contents info)
302 "Transcode BLOCK object into Beamer code.
303 CONTENTS is the text being bold. INFO is a plist used as
304 a communication channel."
305 (format "\\alert%s{%s}"
306 (or (org-e-beamer--element-has-overlay-p bold) "")
307 contents))
310 ;;;; Export Block
312 (defun org-e-beamer-export-block (export-block contents info)
313 "Transcode an EXPORT-BLOCK element into Beamer code.
314 CONTENTS is nil. INFO is a plist used as a communication
315 channel."
316 (when (member (org-element-property :type export-block) '("BEAMER" "LATEX"))
317 (org-remove-indentation (org-element-property :value export-block))))
320 ;;;; Export Snippet
322 (defun org-e-beamer-export-snippet (export-snippet contents info)
323 "Transcode an EXPORT-SNIPPET object into Beamer code.
324 CONTENTS is nil. INFO is a plist used as a communication
325 channel."
326 (let ((backend (org-export-snippet-backend export-snippet))
327 (value (org-element-property :value export-snippet)))
328 ;; Only "e-latex" and "e-beamer" snippets are retained.
329 (cond ((eq backend 'e-latex) value)
330 ;; Ignore "e-beamer" snippets specifying overlays.
331 ((and (eq backend 'e-beamer)
332 (or (org-export-get-previous-element export-snippet info)
333 (not (string-match "\\`<.*>\\'" value))))
334 value))))
337 ;;;; Headline
339 ;; The main function to translate an headline is
340 ;; `org-e-beamer-headline'.
342 ;; Depending on the level at which an headline is considered as
343 ;; a frame (given by `org-e-beamer--frame-level'), the headline is
344 ;; either a section (`org-e-beamer--format-section'), a frame
345 ;; (`org-e-beamer--format-frame') or a block
346 ;; (`org-e-beamer--format-block').
348 ;; `org-e-beamer-headline' also takes care of special environments
349 ;; like "ignoreheading", "note", "noteNH", "appendix" and
350 ;; "againframe".
352 (defun org-e-beamer--get-label (headline info)
353 "Return label for HEADLINE, as a string.
355 INFO is a plist used as a communication channel.
357 The value is either the label specified in \"BEAMER_opt\"
358 property, or a fallback value built from headline's number. This
359 function assumes HEADLINE will be treated as a frame."
360 (let ((opt (org-element-property :beamer-opt headline)))
361 (if (and (org-string-nw-p opt)
362 (string-match "\\(?:^\\|,\\)label=\\(.*?\\)\\(?:$\\|,\\)" opt))
363 (match-string 1 opt)
364 (format "sec-%s"
365 (mapconcat 'number-to-string
366 (org-export-get-headline-number headline info)
367 "-")))))
369 (defun org-e-beamer--frame-level (headline info)
370 "Return frame level in subtree containing HEADLINE.
371 INFO is a plist used as a communication channel."
373 ;; 1. Look for "frame" environment in parents, starting from the
374 ;; farthest.
375 (catch 'exit
376 (mapc (lambda (parent)
377 (let ((env (org-element-property :beamer-env parent)))
378 (when (and env (member (downcase env) '("frame" "fullframe")))
379 (throw 'exit (org-export-get-relative-level parent info)))))
380 (nreverse (org-export-get-genealogy headline)))
381 nil)
382 ;; 2. Look for "frame" environment in HEADLINE.
383 (let ((env (org-element-property :beamer-env headline)))
384 (and env (member (downcase env) '("frame" "fullframe"))
385 (org-export-get-relative-level headline info)))
386 ;; 3. Look for "frame" environment in sub-tree.
387 (org-element-map
388 headline 'headline
389 (lambda (hl)
390 (let ((env (org-element-property :beamer-env hl)))
391 (when (and env (member (downcase env) '("frame" "fullframe")))
392 (org-export-get-relative-level hl info))))
393 info 'first-match)
394 ;; 4. No "frame" environment in tree: use default value.
395 (plist-get info :headline-levels)))
397 (defun org-e-beamer--format-section (headline contents info)
398 "Format HEADLINE as a sectioning part.
399 CONTENTS holds the contents of the headline. INFO is a plist
400 used as a communication channel."
401 ;; Use `e-latex' back-end output, inserting overlay specifications
402 ;; if possible.
403 (let ((latex-headline (org-export-with-backend 'e-latex headline contents info))
404 (mode-specs (org-element-property :beamer-act headline)))
405 (if (and mode-specs
406 (string-match "\\`\\\\\\(.*?\\)\\(?:\\*\\|\\[.*\\]\\)?{"
407 latex-headline))
408 (replace-match (concat (match-string 1 latex-headline)
409 (format "<%s>" mode-specs))
410 nil nil latex-headline 1)
411 latex-headline)))
413 (defun org-e-beamer--format-frame (headline contents info)
414 "Format HEADLINE as a frame.
415 CONTENTS holds the contents of the headline. INFO is a plist
416 used as a communication channel."
417 (let ((fragilep
418 ;; FRAGILEP is non-nil when HEADLINE contains an element
419 ;; among `org-e-beamer-verbatim-elements'.
420 (org-element-map headline org-e-beamer-verbatim-elements 'identity
421 info 'first-match)))
422 (concat "\\begin{frame}"
423 ;; Overlay specification, if any. When surrounded by
424 ;; square brackets, consider it as a default
425 ;; specification.
426 (let ((action (org-element-property :beamer-act headline)))
427 (cond
428 ((not action) "")
429 ((string-match "\\`\\[.*\\]\\'" action )
430 (org-e-beamer--normalize-argument action 'defaction))
431 (t (org-e-beamer--normalize-argument action 'action))))
432 ;; Options, if any.
433 (let* ((beamer-opt (org-element-property :beamer-opt headline))
434 (options
435 ;; Collect options from default value and headline's
436 ;; properties. Also add a label for links.
437 (append
438 (org-split-string org-e-beamer-frame-default-options ",")
439 (and beamer-opt
440 (org-split-string
441 ;; Remove square brackets if user provided
442 ;; them.
443 (and (string-match "^\\[?\\(.*\\)\\]?$" beamer-opt)
444 (match-string 1 beamer-opt))
445 ","))
446 ;; Provide an automatic label for the frame
447 ;; unless the user specified one.
448 (unless (and beamer-opt
449 (string-match "\\(^\\|,\\)label=" beamer-opt))
450 (list
451 (format "label=%s"
452 (org-e-beamer--get-label headline info)))))))
453 ;; Change options list into a string.
454 (org-e-beamer--normalize-argument
455 (mapconcat
456 'identity
457 (if (or (not fragilep) (member "fragile" options)) options
458 (cons "fragile" options))
459 ",")
460 'option))
461 ;; Title.
462 (let ((env (org-element-property :beamer-env headline)))
463 (format "{%s}"
464 (if (and env (equal (downcase env) "fullframe")) ""
465 (org-export-data
466 (org-element-property :title headline) info))))
467 "\n"
468 ;; The following workaround is required in fragile frames
469 ;; as Beamer will append "\par" to the beginning of the
470 ;; contents. So we need to make sure the command is
471 ;; separated from the contents by at least one space. If
472 ;; it isn't, it will create "\parfirst-word" command and
473 ;; remove the first word from the contents in the PDF
474 ;; output.
475 (if (not fragilep) contents
476 (replace-regexp-in-string "\\`\n*" "\\& " contents))
477 "\\end{frame}")))
479 (defun org-e-beamer--format-block (headline contents info)
480 "Format HEADLINE as a block.
481 CONTENTS holds the contents of the headline. INFO is a plist
482 used as a communication channel."
483 (let* ((column-width (org-element-property :beamer-col headline))
484 ;; ENVIRONMENT defaults to "block" if none is specified and
485 ;; there is no column specification. If there is a column
486 ;; specified but still no explicit environment, ENVIRONMENT
487 ;; is "column".
488 (environment (let ((env (org-element-property :beamer-env headline)))
489 (cond
490 ;; "block" is the fallback environment.
491 ((and (not env) (not column-width)) "block")
492 ;; "column" only.
493 ((not env) "column")
494 ;; Use specified environment.
495 (t (downcase env)))))
496 (env-format (unless (member environment '("column" "columns"))
497 (assoc environment
498 (append org-e-beamer-environments-special
499 org-e-beamer-environments-extra
500 org-e-beamer-environments-default))))
501 (title (org-export-data (org-element-property :title headline) info))
502 (options (let ((options (org-element-property :beamer-opt headline)))
503 (if (not options) ""
504 (org-e-beamer--normalize-argument options 'option))))
505 ;; Start a "columns" environment when explicitly requested or
506 ;; when there is no previous headline or the previous
507 ;; headline do not have a BEAMER_column property.
508 (parent-env (org-element-property
509 :beamer-env (org-export-get-parent-headline headline)))
510 (start-columns-p
511 (or (equal environment "columns")
512 (and column-width
513 (not (and parent-env
514 (equal (downcase parent-env) "columns")))
515 (or (org-export-first-sibling-p headline info)
516 (not (org-element-property
517 :beamer-col
518 (org-export-get-previous-element
519 headline info)))))))
520 ;; End the "columns" environment when explicitly requested or
521 ;; when there is no next headline or the next headline do not
522 ;; have a BEAMER_column property.
523 (end-columns-p
524 (or (equal environment "columns")
525 (and column-width
526 (not (and parent-env
527 (equal (downcase parent-env) "columns")))
528 (or (org-export-last-sibling-p headline info)
529 (not (org-element-property
530 :beamer-col
531 (org-export-get-next-element headline info))))))))
532 (concat
533 (when start-columns-p
534 ;; Column can accept options only when the environment is
535 ;; explicitly defined.
536 (if (not (equal environment "columns")) "\\begin{columns}\n"
537 (format "\\begin{columns}%s\n" options)))
538 (when column-width
539 (format "\\begin{column}%s{%s}\n"
540 ;; One can specify placement for column only when
541 ;; HEADLINE stands for a column on its own.
542 (if (equal environment "column") options "")
543 (format "%s\\textwidth" column-width)))
544 ;; Block's opening string.
545 (when env-format
546 (concat
547 (org-fill-template
548 (nth 2 env-format)
549 (nconc
550 ;; If BEAMER_act property has its value enclosed in square
551 ;; brackets, it is a default overlay specification and
552 ;; overlay specification is empty. Otherwise, it is an
553 ;; overlay specification and the default one is nil.
554 (let ((action (org-element-property :beamer-act headline)))
555 (cond
556 ((not action) (list (cons "a" "") (cons "A" "")))
557 ((string-match "\\`\\[.*\\]\\'" action)
558 (list
559 (cons "A" (org-e-beamer--normalize-argument action 'defaction))
560 (cons "a" "")))
562 (list (cons "a" (org-e-beamer--normalize-argument action 'action))
563 (cons "A" "")))))
564 (list (cons "o" options)
565 (cons "h" title)
566 (cons "H" (if (equal title "") "" (format "{%s}" title)))
567 (cons "U" (if (equal title "") "" (format "[%s]" title))))))
568 "\n"))
569 contents
570 ;; Block's closing string.
571 (when environment (concat (nth 3 env-format) "\n"))
572 (when column-width "\\end{column}\n")
573 (when end-columns-p "\\end{columns}"))))
575 (defun org-e-beamer-headline (headline contents info)
576 "Transcode HEADLINE element into Beamer code.
577 CONTENTS is the contents of the headline. INFO is a plist used
578 as a communication channel."
579 (unless (org-element-property :footnote-section-p headline)
580 (let ((level (org-export-get-relative-level headline info))
581 (frame-level (org-e-beamer--frame-level headline info))
582 (environment (let ((env (org-element-property :beamer-env headline)))
583 (if (stringp env) (downcase env) "block"))))
584 (cond
585 ;; Case 1: Resume frame specified by "BEAMER_ref" property.
586 ((equal environment "againframe")
587 (let ((ref (org-element-property :beamer-ref headline)))
588 ;; Reference to frame being resumed is mandatory. Ignore
589 ;; the whole headline if it isn't provided.
590 (when (org-string-nw-p ref)
591 (concat "\\againframe"
592 ;; Overlay specification.
593 (let ((overlay (org-element-property :beamer-act headline)))
594 (when overlay
595 (org-e-beamer--normalize-argument
596 overlay
597 (if (string-match "^\\[.*\\]$" overlay) 'defaction
598 'action))))
599 ;; Options.
600 (let ((options (org-element-property :beamer-opt headline)))
601 (when options
602 (org-e-beamer--normalize-argument options 'option)))
603 ;; Resolve reference provided by "BEAMER_ref"
604 ;; property. This is done by building a minimal fake
605 ;; link and calling the appropriate resolve function,
606 ;; depending on the reference syntax.
607 (let* ((type
608 (progn
609 (string-match "^\\(id:\\|#\\|\\*\\)?\\(.*\\)" ref)
610 (cond
611 ((or (not (match-string 1 ref))
612 (equal (match-string 1 ref) "*")) 'fuzzy)
613 ((equal (match-string 1 ref) "id:") 'id)
614 (t 'custom-id))))
615 (link (list 'link (list :path (match-string 2 ref))))
616 (target (if (eq type 'fuzzy)
617 (org-export-resolve-fuzzy-link link info)
618 (org-export-resolve-id-link link info))))
619 ;; Now use user-defined label provided in TARGET
620 ;; headline, or fallback to standard one.
621 (format "{%s}" (org-e-beamer--get-label target info)))))))
622 ;; Case 2: Creation of an appendix is requested.
623 ((equal environment "appendix")
624 (concat "\\appendix"
625 (org-element-property :beamer-act headline)
626 "\n"
627 (make-string (org-element-property :pre-blank headline) ?\n)
628 contents))
629 ;; Case 3: Ignore heading.
630 ((equal environment "ignoreheading")
631 (concat (make-string (org-element-property :pre-blank headline) ?\n)
632 contents))
633 ;; Case 4: HEADLINE is a note.
634 ((member environment '("note" "noteNH"))
635 (format "\\note{%s}"
636 (concat (and (equal environment "note")
637 (concat
638 (org-export-data
639 (org-element-property :title headline) info)
640 "\n"))
641 (org-trim contents))))
642 ;; Case 5: HEADLINE is a frame.
643 ((= level frame-level)
644 (org-e-beamer--format-frame headline contents info))
645 ;; Case 6: Regular section, extracted from
646 ;; `org-e-latex-classes'.
647 ((< level frame-level)
648 (org-e-beamer--format-section headline contents info))
649 ;; Case 7: Otherwise, HEADLINE is a block.
650 (t (org-e-beamer--format-block headline contents info))))))
653 ;;;; Item
655 (defun org-e-beamer-item (item contents info)
656 "Transcode an ITEM element into Beamer code.
657 CONTENTS holds the contents of the item. INFO is a plist holding
658 contextual information."
659 (let ((action (let ((first-element (car (org-element-contents item))))
660 (and (eq (org-element-type first-element) 'paragraph)
661 (org-e-beamer--element-has-overlay-p first-element))))
662 (output (org-export-with-backend 'e-latex item contents info)))
663 (if (not action) output
664 ;; If the item starts with a paragraph and that paragraph starts
665 ;; with an export snippet specifying an overlay, insert it after
666 ;; \item command.
667 (replace-regexp-in-string "\\\\item" (concat "\\\\item" action) output))))
670 ;;;; Keyword
672 (defun org-e-beamer-keyword (keyword contents info)
673 "Transcode a KEYWORD element into Beamer code.
674 CONTENTS is nil. INFO is a plist used as a communication
675 channel."
676 (let ((key (org-element-property :key keyword))
677 (value (org-element-property :value keyword)))
678 ;; Handle specifically BEAMER and TOC (headlines only) keywords.
679 ;; Otherwise, fallback to `e-latex' back-end.
680 (cond
681 ((equal key "BEAMER") value)
682 ((and (equal key "TOC") (string-match "\\<headlines\\>" value))
683 (let ((depth (or (and (string-match "[0-9]+" value)
684 (string-to-number (match-string 0 value)))
685 (plist-get info :with-toc)))
686 (options (and (string-match "\\[.*?\\]" value)
687 (match-string 0 value))))
688 (concat
689 "\\begin{frame}"
690 (when (wholenump depth) (format "\\setcounter{tocdepth}{%s}\n" depth))
691 "\\tableofcontents" options "\n"
692 "\\end{frame}")))
693 (t (org-export-with-backend 'e-latex keyword contents info)))))
696 ;;;; Link
698 (defun org-e-beamer-link (link contents info)
699 "Transcode a LINK object into Beamer code.
700 CONTENTS is the description part of the link. INFO is a plist
701 used as a communication channel."
702 (let ((type (org-element-property :type link))
703 (path (org-element-property :path link)))
704 ;; Use \hyperlink command for all internal links.
705 (cond
706 ((equal type "radio")
707 (let ((destination (org-export-resolve-radio-link link info)))
708 (when destination
709 (format "\\hyperlink%s{%s}{%s}"
710 (or (org-e-beamer--element-has-overlay-p link) "")
711 (org-export-solidify-link-text path)
712 (org-export-data (org-element-contents destination) info)))))
713 ((and (member type '("custom-id" "fuzzy" "id"))
714 (let ((destination (if (string= type "fuzzy")
715 (org-export-resolve-fuzzy-link link info)
716 (org-export-resolve-id-link link info))))
717 (case (org-element-type destination)
718 (headline
719 (let ((label
720 (format "sec-%s"
721 (mapconcat
722 'number-to-string
723 (org-export-get-headline-number
724 destination info)
725 "-"))))
726 (if (and (plist-get info :section-numbers) (not contents))
727 (format "\\ref{%s}" label)
728 (format "\\hyperlink%s{%s}{%s}"
729 (or (org-e-beamer--element-has-overlay-p link) "")
730 label
731 contents))))
732 (target
733 (let ((path (org-export-solidify-link-text path)))
734 (if (not contents) (format "\\ref{%s}" path)
735 (format "\\hyperlink%s{%s}{%s}"
736 (or (org-e-beamer--element-has-overlay-p link) "")
737 path
738 contents))))))))
739 ;; Otherwise, use `e-latex' back-end.
740 (t (org-export-with-backend 'e-latex link contents info)))))
743 ;;;; Plain List
745 ;; Plain lists support `:overlay' (for any type), `:template' (for
746 ;; ordered lists only) and `:long-text' (for description lists only)
747 ;; attributes.
749 (defun org-e-beamer-plain-list (plain-list contents info)
750 "Transcode a PLAIN-LIST element into Beamer code.
751 CONTENTS is the contents of the list. INFO is a plist holding
752 contextual information."
753 (let* ((type (org-element-property :type plain-list))
754 (attributes (org-export-read-attribute :attr_beamer plain-list))
755 (latex-type (cond ((eq type 'ordered) "enumerate")
756 ((eq type 'descriptive) "description")
757 (t "itemize"))))
758 (org-e-latex--wrap-label
759 plain-list
760 (format "\\begin{%s}%s%s\n%s\\end{%s}"
761 latex-type
762 ;; Default overlay specification, if any.
763 (let ((overlay (plist-get attributes :overlay)))
764 (if (not overlay) ""
765 (org-e-beamer--normalize-argument overlay 'defaction)))
766 ;; Second optional argument depends on the list type.
767 (case type
768 (ordered
769 (let ((template (plist-get attributes :template)))
770 (if (not template) ""
771 (org-e-beamer--normalize-argument template 'option))))
772 (descriptive
773 (let ((long-text (plist-get attributes :long-text)))
774 (if (not long-text) ""
775 (org-e-beamer--normalize-argument long-text 'option))))
776 ;; There's no second argument for un-ordered lists.
777 (otherwise ""))
778 ;; Eventually insert contents and close environment.
779 contents
780 latex-type))))
783 ;;;; Radio Target
785 (defun org-e-beamer-radio-target (radio-target text info)
786 "Transcode a RADIO-TARGET object into Beamer code.
787 TEXT is the text of the target. INFO is a plist holding
788 contextual information."
789 (format "\\hypertarget%s{%s}{%s}"
790 (or (org-e-beamer--element-has-overlay-p radio-target) "")
791 (org-export-solidify-link-text
792 (org-element-property :value radio-target))
793 text))
796 ;;;; Target
798 (defun org-e-beamer-target (target contents info)
799 "Transcode a TARGET object into Beamer code.
800 CONTENTS is nil. INFO is a plist holding contextual
801 information."
802 (format "\\hypertarget{%s}{}"
803 (org-export-solidify-link-text (org-element-property :value target))))
806 ;;;; Template
808 ;; Template used is similar to the one used in `e-latex' back-end,
809 ;; excepted for the table of contents and Beamer themes.
811 (defun org-e-beamer-template (contents info)
812 "Return complete document string after Beamer conversion.
813 CONTENTS is the transcoded contents string. INFO is a plist
814 holding export options."
815 (let ((title (org-export-data (plist-get info :title) info)))
816 (concat
817 ;; 1. Time-stamp.
818 (and (plist-get info :time-stamp-file)
819 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
820 ;; 2. Document class and packages.
821 (let ((class (plist-get info :latex-class))
822 (class-options (plist-get info :latex-class-options)))
823 (org-element-normalize-string
824 (let* ((header (nth 1 (assoc class org-e-latex-classes)))
825 (document-class-string
826 (and (stringp header)
827 (if (not class-options) header
828 (replace-regexp-in-string
829 "^[ \t]*\\\\documentclass\\(\\(\\[.*\\]\\)?\\)"
830 class-options header t nil 1)))))
831 (when document-class-string
832 (org-e-latex--guess-babel-language
833 (org-e-latex--guess-inputenc
834 (org-splice-latex-header
835 document-class-string
836 org-export-latex-default-packages-alist ; defined in org.el
837 org-export-latex-packages-alist nil ; defined in org.el
838 (plist-get info :latex-header-extra)))
839 info)))))
840 ;; 3. Insert themes.
841 (let ((format-theme
842 (function
843 (lambda (prop command)
844 (let ((theme (plist-get info prop)))
845 (when theme
846 (concat command
847 (if (not (string-match "\\[.*\\]" theme))
848 (format "{%s}\n" theme)
849 (format "%s{%s}\n"
850 (match-string 0 theme)
851 (org-trim
852 (replace-match "" nil nil theme)))))))))))
853 (mapconcat (lambda (args) (apply format-theme args))
854 '((:beamer-theme "\\usetheme")
855 (:beamer-color-theme "\\usecolortheme")
856 (:beamer-font-theme "\\usefonttheme")
857 (:beamer-inner-theme "\\useinnertheme")
858 (:beamer-outer-theme "\\useoutertheme"))
859 ""))
860 ;; 4. Possibly limit depth for headline numbering.
861 (let ((sec-num (plist-get info :section-numbers)))
862 (when (integerp sec-num)
863 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
864 ;; 5. Author.
865 (let ((author (and (plist-get info :with-author)
866 (let ((auth (plist-get info :author)))
867 (and auth (org-export-data auth info)))))
868 (email (and (plist-get info :with-email)
869 (org-export-data (plist-get info :email) info))))
870 (cond ((and author email (not (string= "" email)))
871 (format "\\author{%s\\thanks{%s}}\n" author email))
872 (author (format "\\author{%s}\n" author))
873 (t "\\author{}\n")))
874 ;; 6. Date.
875 (let ((date (and (plist-get info :with-date)
876 (org-export-data (plist-get info :date) info))))
877 (format "\\date{%s}\n" (or date "")))
878 ;; 7. Title
879 (format "\\title{%s}\n" title)
880 ;; 8. Hyperref options.
881 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
882 (or (plist-get info :keywords) "")
883 (or (plist-get info :description) "")
884 (if (not (plist-get info :with-creator)) ""
885 (plist-get info :creator)))
886 ;; 9. Document start.
887 "\\begin{document}\n\n"
888 ;; 10. Title command.
889 (org-element-normalize-string
890 (cond ((string= "" title) nil)
891 ((not (stringp org-e-latex-title-command)) nil)
892 ((string-match "\\(?:[^%]\\|^\\)%s"
893 org-e-latex-title-command)
894 (format org-e-latex-title-command title))
895 (t org-e-latex-title-command)))
896 ;; 11. Table of contents.
897 (let ((depth (plist-get info :with-toc)))
898 (when depth
899 (concat
900 (format "\\begin{frame}%s{%s}\n"
901 (org-e-beamer--normalize-argument
902 org-e-beamer-outline-frame-options 'option)
903 org-e-beamer-outline-frame-title)
904 (when (wholenump depth)
905 (format "\\setcounter{tocdepth}{%d}\n" depth))
906 "\\tableofcontents\n"
907 "\\end{frame}\n\n")))
908 ;; 12. Document's body.
909 contents
910 ;; 13. Creator.
911 (let ((creator-info (plist-get info :with-creator)))
912 (cond
913 ((not creator-info) "")
914 ((eq creator-info 'comment)
915 (format "%% %s\n" (plist-get info :creator)))
916 (t (concat (plist-get info :creator) "\n"))))
917 ;; 14. Document end.
918 "\\end{document}")))
922 ;;; Minor Mode
925 (defvar org-e-beamer-mode-map (make-sparse-keymap)
926 "The keymap for `org-e-beamer-mode'.")
927 (define-key org-e-beamer-mode-map "\C-c\C-b" 'org-e-beamer-select-environment)
929 ;;;###autoload
930 (define-minor-mode org-e-beamer-mode
931 "Support for editing Beamer oriented Org mode files."
932 nil " Bm" 'org-e-beamer-mode-map)
934 (when (fboundp 'font-lock-add-keywords)
935 (font-lock-add-keywords
936 'org-mode
937 '((":\\(B_[a-z]+\\|BMCOL\\):" 1 'org-e-beamer-tag prepend))
938 'prepend))
940 (defface org-e-beamer-tag '((t (:box (:line-width 1 :color grey40))))
941 "The special face for beamer tags."
942 :group 'org-export-e-beamer)
944 (defun org-e-beamer-property-changed (property value)
945 "Track the BEAMER_env property with tags.
946 PROPERTY is the name of the modified property. VALUE is its new
947 value."
948 (cond
949 ((equal property "BEAMER_env")
950 (save-excursion
951 (org-back-to-heading t)
952 ;; Filter out Beamer-related tags and install environment tag.
953 (let ((tags (org-remove-if (lambda (x) (string-match "^B_" x))
954 (org-get-tags)))
955 (env-tag (and (org-string-nw-p value) (concat "B_" value))))
956 (org-set-tags-to (if env-tag (cons env-tag tags) tags))
957 (when env-tag (org-toggle-tag env-tag 'on)))))
958 ((equal property "BEAMER_col")
959 (org-toggle-tag "BMCOL" (if (org-string-nw-p value) 'on 'off)))))
961 (add-hook 'org-property-changed-functions 'org-e-beamer-property-changed)
963 (defun org-e-beamer-allowed-property-values (property)
964 "Supply allowed values for PROPERTY."
965 (cond
966 ((and (equal property "BEAMER_env")
967 (not (org-entry-get nil (concat property "_ALL") 'inherit)))
968 ;; If no allowed values for BEAMER_env have been defined,
969 ;; supply all defined environments
970 (mapcar 'car (append org-e-beamer-environments-special
971 org-e-beamer-environments-extra
972 org-e-beamer-environments-default)))
973 ((and (equal property "BEAMER_col")
974 (not (org-entry-get nil (concat property "_ALL") 'inherit)))
975 ;; If no allowed values for BEAMER_col have been defined,
976 ;; supply some
977 (org-split-string org-e-beamer-column-widths " "))))
979 (add-hook 'org-property-allowed-value-functions
980 'org-e-beamer-allowed-property-values)
984 ;;; Commands
986 ;;;###autoload
987 (defun org-e-beamer-export-as-latex
988 (&optional subtreep visible-only body-only ext-plist)
989 "Export current buffer as a Beamer buffer.
991 If narrowing is active in the current buffer, only export its
992 narrowed part.
994 If a region is active, export that region.
996 When optional argument SUBTREEP is non-nil, export the sub-tree
997 at point, extracting information from the headline properties
998 first.
1000 When optional argument VISIBLE-ONLY is non-nil, don't export
1001 contents of hidden elements.
1003 When optional argument BODY-ONLY is non-nil, only write code
1004 between \"\\begin{document}\" and \"\\end{document}\".
1006 EXT-PLIST, when provided, is a property list with external
1007 parameters overriding Org default settings, but still inferior to
1008 file-local settings.
1010 Export is done in a buffer named \"*Org E-BEAMER Export*\", which
1011 will be displayed when `org-export-show-temporary-export-buffer'
1012 is non-nil."
1013 (interactive)
1014 (let ((outbuf (org-export-to-buffer
1015 'e-beamer "*Org E-BEAMER Export*"
1016 subtreep visible-only body-only ext-plist)))
1017 (with-current-buffer outbuf (LaTeX-mode))
1018 (when org-export-show-temporary-export-buffer
1019 (switch-to-buffer-other-window outbuf))))
1021 ;;;###autoload
1022 (defun org-e-beamer-export-to-latex
1023 (&optional subtreep visible-only body-only ext-plist pub-dir)
1024 "Export current buffer as a Beamer presentation (tex).
1026 If narrowing is active in the current buffer, only export its
1027 narrowed part.
1029 If a region is active, export that region.
1031 When optional argument SUBTREEP is non-nil, export the sub-tree
1032 at point, extracting information from the headline properties
1033 first.
1035 When optional argument VISIBLE-ONLY is non-nil, don't export
1036 contents of hidden elements.
1038 When optional argument BODY-ONLY is non-nil, only write code
1039 between \"\\begin{document}\" and \"\\end{document}\".
1041 EXT-PLIST, when provided, is a property list with external
1042 parameters overriding Org default settings, but still inferior to
1043 file-local settings.
1045 When optional argument PUB-DIR is set, use it as the publishing
1046 directory.
1048 Return output file's name."
1049 (interactive)
1050 (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir)))
1051 (org-export-to-file
1052 'e-beamer outfile subtreep visible-only body-only ext-plist)))
1054 ;;;###autoload
1055 (defun org-e-beamer-export-to-pdf
1056 (&optional subtreep visible-only body-only ext-plist pub-dir)
1057 "Export current buffer as a Beamer presentation (PDF).
1059 If narrowing is active in the current buffer, only export its
1060 narrowed part.
1062 If a region is active, export that region.
1064 When optional argument SUBTREEP is non-nil, export the sub-tree
1065 at point, extracting information from the headline properties
1066 first.
1068 When optional argument VISIBLE-ONLY is non-nil, don't export
1069 contents of hidden elements.
1071 When optional argument BODY-ONLY is non-nil, only write code
1072 between \"\\begin{document}\" and \"\\end{document}\".
1074 EXT-PLIST, when provided, is a property list with external
1075 parameters overriding Org default settings, but still inferior to
1076 file-local settings.
1078 When optional argument PUB-DIR is set, use it as the publishing
1079 directory.
1081 Return PDF file's name."
1082 (interactive)
1083 (org-e-latex-compile
1084 (org-e-beamer-export-to-latex
1085 subtreep visible-only body-only ext-plist pub-dir)))
1087 ;;;###autoload
1088 (defun org-e-beamer-select-environment ()
1089 "Select the environment to be used by beamer for this entry.
1090 While this uses (for convenience) a tag selection interface, the
1091 result of this command will be that the BEAMER_env *property* of
1092 the entry is set.
1094 In addition to this, the command will also set a tag as a visual
1095 aid, but the tag does not have any semantic meaning."
1096 (interactive)
1097 ;; Make sure `org-e-beamer-environments-special' has a higher
1098 ;; priority than `org-e-beamer-environments-extra'.
1099 (let* ((envs (append org-e-beamer-environments-special
1100 org-e-beamer-environments-extra
1101 org-e-beamer-environments-default))
1102 (org-tag-alist
1103 (append '((:startgroup))
1104 (mapcar (lambda (e) (cons (concat "B_" (car e))
1105 (string-to-char (nth 1 e))))
1106 envs)
1107 '((:endgroup))
1108 '(("BMCOL" . ?|))))
1109 (org-fast-tag-selection-single-key t))
1110 (org-set-tags)
1111 (let ((tags (or (ignore-errors (org-get-tags-string)) "")))
1112 (cond
1113 ;; For a column, automatically ask for its width.
1114 ((eq org-last-tag-selection-key ?|)
1115 (if (string-match ":BMCOL:" tags)
1116 (org-set-property "BEAMER_col" (read-string "Column width: "))
1117 (org-delete-property "BEAMER_col")))
1118 ;; For an "againframe" section, automatically ask for reference
1119 ;; to resumed frame and overlay specifications.
1120 ((eq org-last-tag-selection-key ?A)
1121 (if (equal (org-entry-get nil "BEAMER_env") "againframe")
1122 (progn (org-entry-delete nil "BEAMER_env")
1123 (org-entry-delete nil "BEAMER_ref")
1124 (org-entry-delete nil "BEAMER_act"))
1125 (org-entry-put nil "BEAMER_env" "againframe")
1126 (org-set-property
1127 "BEAMER_ref"
1128 (read-string "Frame reference (*Title, #custom-id, id:...): "))
1129 (org-set-property "BEAMER_act"
1130 (read-string "Overlay specification: "))))
1131 ((string-match (concat ":B_\\(" (mapconcat 'car envs "\\|") "\\):") tags)
1132 (org-entry-put nil "BEAMER_env" (match-string 1 tags)))
1133 (t (org-entry-delete nil "BEAMER_env"))))))
1135 ;;;###autoload
1136 (defun org-e-beamer-insert-options-template (&optional kind)
1137 "Insert a settings template, to make sure users do this right."
1138 (interactive (progn
1139 (message "Current [s]ubtree or [g]lobal?")
1140 (if (eq (read-char-exclusive) ?g) (list 'global)
1141 (list 'subtree))))
1142 (if (eq kind 'subtree)
1143 (progn
1144 (org-back-to-heading t)
1145 (org-reveal)
1146 (org-entry-put nil "EXPORT_LaTeX_CLASS" "beamer")
1147 (org-entry-put nil "EXPORT_LaTeX_CLASS_OPTIONS" "[presentation]")
1148 (org-entry-put nil "EXPORT_FILE_NAME" "presentation.pdf")
1149 (when org-e-beamer-column-view-format
1150 (org-entry-put nil "COLUMNS" org-e-beamer-column-view-format))
1151 (org-entry-put nil "BEAMER_col_ALL" org-e-beamer-column-widths))
1152 (insert "#+LaTeX_CLASS: beamer\n")
1153 (insert "#+LaTeX_CLASS_OPTIONS: [presentation]\n")
1154 (when org-e-beamer-theme
1155 (insert "#+BEAMER_THEME: " org-e-beamer-theme "\n"))
1156 (when org-e-beamer-column-view-format
1157 (insert "#+COLUMNS: " org-e-beamer-column-view-format "\n"))
1158 (insert "#+PROPERTY: BEAMER_col_ALL " org-e-beamer-column-widths "\n")))
1161 (provide 'org-e-beamer)
1162 ;;; org-e-beamer.el ends here