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