Merge export and special blocks within back-ends
[org-mode.git] / lisp / ox-beamer.el
blob8b2c12070e83b0a423112fbe8865ee07240c510f
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 '((:headline-levels nil "H" org-beamer-frame-level)
235 (:latex-class "LATEX_CLASS" nil "beamer" t)
236 (:beamer-column-view-format "COLUMNS" nil org-beamer-column-view-format)
237 (:beamer-theme "BEAMER_THEME" nil org-beamer-theme)
238 (:beamer-color-theme "BEAMER_COLOR_THEME" nil nil t)
239 (:beamer-font-theme "BEAMER_FONT_THEME" nil nil t)
240 (:beamer-inner-theme "BEAMER_INNER_THEME" nil nil t)
241 (:beamer-outer-theme "BEAMER_OUTER_THEME" nil nil t)
242 (:beamer-header-extra "BEAMER_HEADER" nil nil newline)
243 (:beamer-environments-extra nil nil org-beamer-environments-extra)
244 (:beamer-frame-default-options nil nil org-beamer-frame-default-options)
245 (:beamer-outline-frame-options nil nil org-beamer-outline-frame-options)
246 (:beamer-outline-frame-title nil nil org-beamer-outline-frame-title))
247 :translate-alist '((bold . org-beamer-bold)
248 (export-snippet . org-beamer-export-snippet)
249 (headline . org-beamer-headline)
250 (item . org-beamer-item)
251 (keyword . org-beamer-keyword)
252 (link . org-beamer-link)
253 (plain-list . org-beamer-plain-list)
254 (radio-target . org-beamer-radio-target)
255 (target . org-beamer-target)
256 (template . org-beamer-template)))
260 ;;; Transcode Functions
262 ;;;; Bold
264 (defun org-beamer-bold (bold contents info)
265 "Transcode BLOCK object into Beamer code.
266 CONTENTS is the text being bold. INFO is a plist used as
267 a communication channel."
268 (format "\\alert%s{%s}"
269 (or (org-beamer--element-has-overlay-p bold) "")
270 contents))
273 ;;;; Export Snippet
275 (defun org-beamer-export-snippet (export-snippet contents info)
276 "Transcode an EXPORT-SNIPPET object into Beamer code.
277 CONTENTS is nil. INFO is a plist used as a communication
278 channel."
279 (let ((backend (org-export-snippet-backend export-snippet))
280 (value (org-element-property :value export-snippet)))
281 ;; Only "latex" and "beamer" snippets are retained.
282 (cond ((eq backend 'latex) value)
283 ;; Ignore "beamer" snippets specifying overlays.
284 ((and (eq backend 'beamer)
285 (or (org-export-get-previous-element export-snippet info)
286 (not (string-match "\\`<.*>\\'" value))))
287 value))))
290 ;;;; Headline
292 ;; The main function to translate a headline is
293 ;; `org-beamer-headline'.
295 ;; Depending on the level at which a headline is considered as
296 ;; a frame (given by `org-beamer--frame-level'), the headline is
297 ;; either a section (`org-beamer--format-section'), a frame
298 ;; (`org-beamer--format-frame') or a block
299 ;; (`org-beamer--format-block').
301 ;; `org-beamer-headline' also takes care of special environments
302 ;; like "ignoreheading", "note", "noteNH", "appendix" and
303 ;; "againframe".
305 (defun org-beamer--get-label (headline info)
306 "Return label for HEADLINE, as a string.
308 INFO is a plist used as a communication channel.
310 The value is either the label specified in \"BEAMER_opt\"
311 property, or a fallback value built from headline's number. This
312 function assumes HEADLINE will be treated as a frame."
313 (let ((opt (org-element-property :BEAMER_OPT headline)))
314 (if (and (org-string-nw-p opt)
315 (string-match "\\(?:^\\|,\\)label=\\(.*?\\)\\(?:$\\|,\\)" opt))
316 (match-string 1 opt)
317 (format "sec-%s"
318 (mapconcat 'number-to-string
319 (org-export-get-headline-number headline info)
320 "-")))))
322 (defun org-beamer--frame-level (headline info)
323 "Return frame level in subtree containing HEADLINE.
324 INFO is a plist used as a communication channel."
326 ;; 1. Look for "frame" environment in parents, starting from the
327 ;; farthest.
328 (catch 'exit
329 (mapc (lambda (parent)
330 (let ((env (org-element-property :BEAMER_ENV parent)))
331 (when (and env (member-ignore-case env '("frame" "fullframe")))
332 (throw 'exit (org-export-get-relative-level parent info)))))
333 (nreverse (org-export-get-genealogy headline)))
334 nil)
335 ;; 2. Look for "frame" environment in HEADLINE.
336 (let ((env (org-element-property :BEAMER_ENV headline)))
337 (and env (member-ignore-case env '("frame" "fullframe"))
338 (org-export-get-relative-level headline info)))
339 ;; 3. Look for "frame" environment in sub-tree.
340 (org-element-map headline 'headline
341 (lambda (hl)
342 (let ((env (org-element-property :BEAMER_ENV hl)))
343 (when (and env (member-ignore-case env '("frame" "fullframe")))
344 (org-export-get-relative-level hl info))))
345 info 'first-match)
346 ;; 4. No "frame" environment in tree: use default value.
347 (plist-get info :headline-levels)))
349 (defun org-beamer--format-section (headline contents info)
350 "Format HEADLINE as a sectioning part.
351 CONTENTS holds the contents of the headline. INFO is a plist
352 used as a communication channel."
353 (let ((latex-headline
354 (org-export-with-backend
355 ;; We create a temporary export back-end which behaves the
356 ;; same as current one, but adds "\protect" in front of the
357 ;; output of some objects.
358 (org-export-create-backend
359 :parent 'latex
360 :transcoders
361 (let ((protected-output
362 (function
363 (lambda (object contents info)
364 (let ((code (org-export-with-backend
365 'beamer object contents info)))
366 (if (org-string-nw-p code) (concat "\\protect" code)
367 code))))))
368 (mapcar #'(lambda (type) (cons type protected-output))
369 '(bold footnote-reference italic strike-through timestamp
370 underline))))
371 headline
372 contents
373 info))
374 (mode-specs (org-element-property :BEAMER_ACT headline)))
375 (if (and mode-specs
376 (string-match "\\`\\\\\\(.*?\\)\\(?:\\*\\|\\[.*\\]\\)?{"
377 latex-headline))
378 ;; Insert overlay specifications.
379 (replace-match (concat (match-string 1 latex-headline)
380 (format "<%s>" mode-specs))
381 nil nil latex-headline 1)
382 latex-headline)))
384 (defun org-beamer--format-frame (headline contents info)
385 "Format HEADLINE as a frame.
386 CONTENTS holds the contents of the headline. INFO is a plist
387 used as a communication channel."
388 (let ((fragilep
389 ;; FRAGILEP is non-nil when HEADLINE contains an element
390 ;; among `org-beamer-verbatim-elements'.
391 (org-element-map headline org-beamer-verbatim-elements 'identity
392 info 'first-match)))
393 (concat "\\begin{frame}"
394 ;; Overlay specification, if any. When surrounded by
395 ;; square brackets, consider it as a default
396 ;; specification.
397 (let ((action (org-element-property :BEAMER_ACT headline)))
398 (cond
399 ((not action) "")
400 ((string-match "\\`\\[.*\\]\\'" action )
401 (org-beamer--normalize-argument action 'defaction))
402 (t (org-beamer--normalize-argument action 'action))))
403 ;; Options, if any.
404 (let* ((beamer-opt (org-element-property :BEAMER_OPT headline))
405 (options
406 ;; Collect options from default value and headline's
407 ;; properties. Also add a label for links.
408 (append
409 (org-split-string org-beamer-frame-default-options ",")
410 (and beamer-opt
411 (org-split-string
412 ;; Remove square brackets if user provided
413 ;; them.
414 (and (string-match "^\\[?\\(.*\\)\\]?$" beamer-opt)
415 (match-string 1 beamer-opt))
416 ","))
417 ;; Provide an automatic label for the frame
418 ;; unless the user specified one.
419 (unless (and beamer-opt
420 (string-match "\\(^\\|,\\)label=" beamer-opt))
421 (list
422 (format "label=%s"
423 (org-beamer--get-label headline info)))))))
424 ;; Change options list into a string.
425 (org-beamer--normalize-argument
426 (mapconcat
427 'identity
428 (if (or (not fragilep) (member "fragile" options)) options
429 (cons "fragile" options))
430 ",")
431 'option))
432 ;; Title.
433 (let ((env (org-element-property :BEAMER_ENV headline)))
434 (format "{%s}"
435 (if (and env (equal (downcase env) "fullframe")) ""
436 (org-export-data
437 (org-element-property :title headline) info))))
438 "\n"
439 ;; The following workaround is required in fragile frames
440 ;; as Beamer will append "\par" to the beginning of the
441 ;; contents. So we need to make sure the command is
442 ;; separated from the contents by at least one space. If
443 ;; it isn't, it will create "\parfirst-word" command and
444 ;; remove the first word from the contents in the PDF
445 ;; output.
446 (if (not fragilep) contents
447 (replace-regexp-in-string "\\`\n*" "\\& " (or contents "")))
448 "\\end{frame}")))
450 (defun org-beamer--format-block (headline contents info)
451 "Format HEADLINE as a block.
452 CONTENTS holds the contents of the headline. INFO is a plist
453 used as a communication channel."
454 (let* ((column-width (org-element-property :BEAMER_COL headline))
455 ;; ENVIRONMENT defaults to "block" if none is specified and
456 ;; there is no column specification. If there is a column
457 ;; specified but still no explicit environment, ENVIRONMENT
458 ;; is "column".
459 (environment (let ((env (org-element-property :BEAMER_ENV headline)))
460 (cond
461 ;; "block" is the fallback environment.
462 ((and (not env) (not column-width)) "block")
463 ;; "column" only.
464 ((not env) "column")
465 ;; Use specified environment.
466 (t env))))
467 (raw-title (org-element-property :raw-value headline))
468 (env-format
469 (cond ((member environment '("column" "columns")) nil)
470 ((assoc environment
471 (append org-beamer-environments-extra
472 org-beamer-environments-default)))
473 (t (user-error "Wrong block type at a headline named \"%s\""
474 raw-title))))
475 (title (org-export-data (org-element-property :title headline) info))
476 (options (let ((options (org-element-property :BEAMER_OPT headline)))
477 (if (not options) ""
478 (org-beamer--normalize-argument options 'option))))
479 ;; Start a "columns" environment when explicitly requested or
480 ;; when there is no previous headline or the previous
481 ;; headline do not have a BEAMER_column property.
482 (parent-env (org-element-property
483 :BEAMER_ENV (org-export-get-parent-headline headline)))
484 (start-columns-p
485 (or (equal environment "columns")
486 (and column-width
487 (not (and parent-env
488 (equal (downcase parent-env) "columns")))
489 (or (org-export-first-sibling-p headline info)
490 (not (org-element-property
491 :BEAMER_COL
492 (org-export-get-previous-element
493 headline info)))))))
494 ;; End the "columns" environment when explicitly requested or
495 ;; when there is no next headline or the next headline do not
496 ;; have a BEAMER_column property.
497 (end-columns-p
498 (or (equal environment "columns")
499 (and column-width
500 (not (and parent-env
501 (equal (downcase parent-env) "columns")))
502 (or (org-export-last-sibling-p headline info)
503 (not (org-element-property
504 :BEAMER_COL
505 (org-export-get-next-element headline info))))))))
506 (concat
507 (when start-columns-p
508 ;; Column can accept options only when the environment is
509 ;; explicitly defined.
510 (if (not (equal environment "columns")) "\\begin{columns}\n"
511 (format "\\begin{columns}%s\n" options)))
512 (when column-width
513 (format "\\begin{column}%s{%s}\n"
514 ;; One can specify placement for column only when
515 ;; HEADLINE stands for a column on its own.
516 (if (equal environment "column") options "")
517 (format "%s\\textwidth" column-width)))
518 ;; Block's opening string.
519 (when (nth 2 env-format)
520 (concat
521 (org-fill-template
522 (nth 2 env-format)
523 (nconc
524 ;; If BEAMER_act property has its value enclosed in square
525 ;; brackets, it is a default overlay specification and
526 ;; overlay specification is empty. Otherwise, it is an
527 ;; overlay specification and the default one is nil.
528 (let ((action (org-element-property :BEAMER_ACT headline)))
529 (cond
530 ((not action) (list (cons "a" "") (cons "A" "")))
531 ((string-match "\\`\\[.*\\]\\'" action)
532 (list
533 (cons "A" (org-beamer--normalize-argument action 'defaction))
534 (cons "a" "")))
536 (list (cons "a" (org-beamer--normalize-argument action 'action))
537 (cons "A" "")))))
538 (list (cons "o" options)
539 (cons "h" title)
540 (cons "r" raw-title)
541 (cons "H" (if (equal raw-title "") ""
542 (format "{%s}" raw-title)))
543 (cons "U" (if (equal raw-title "") ""
544 (format "[%s]" raw-title))))))
545 "\n"))
546 contents
547 ;; Block's closing string, if any.
548 (and (nth 3 env-format) (concat (nth 3 env-format) "\n"))
549 (when column-width "\\end{column}\n")
550 (when end-columns-p "\\end{columns}"))))
552 (defun org-beamer-headline (headline contents info)
553 "Transcode HEADLINE element into Beamer code.
554 CONTENTS is the contents of the headline. INFO is a plist used
555 as a communication channel."
556 (unless (org-element-property :footnote-section-p headline)
557 (let ((level (org-export-get-relative-level headline info))
558 (frame-level (org-beamer--frame-level headline info))
559 (environment (let ((env (org-element-property :BEAMER_ENV headline)))
560 (or (org-string-nw-p env) "block"))))
561 (cond
562 ;; Case 1: Resume frame specified by "BEAMER_ref" property.
563 ((equal environment "againframe")
564 (let ((ref (org-element-property :BEAMER_REF headline)))
565 ;; Reference to frame being resumed is mandatory. Ignore
566 ;; the whole headline if it isn't provided.
567 (when (org-string-nw-p ref)
568 (concat "\\againframe"
569 ;; Overlay specification.
570 (let ((overlay (org-element-property :BEAMER_ACT headline)))
571 (when overlay
572 (org-beamer--normalize-argument
573 overlay
574 (if (string-match "^\\[.*\\]$" overlay) 'defaction
575 'action))))
576 ;; Options.
577 (let ((options (org-element-property :BEAMER_OPT headline)))
578 (when options
579 (org-beamer--normalize-argument options 'option)))
580 ;; Resolve reference provided by "BEAMER_ref"
581 ;; property. This is done by building a minimal fake
582 ;; link and calling the appropriate resolve function,
583 ;; depending on the reference syntax.
584 (let* ((type
585 (progn
586 (string-match "^\\(id:\\|#\\|\\*\\)?\\(.*\\)" ref)
587 (cond
588 ((or (not (match-string 1 ref))
589 (equal (match-string 1 ref) "*")) 'fuzzy)
590 ((equal (match-string 1 ref) "id:") 'id)
591 (t 'custom-id))))
592 (link (list 'link (list :path (match-string 2 ref))))
593 (target (if (eq type 'fuzzy)
594 (org-export-resolve-fuzzy-link link info)
595 (org-export-resolve-id-link link info))))
596 ;; Now use user-defined label provided in TARGET
597 ;; headline, or fallback to standard one.
598 (format "{%s}" (org-beamer--get-label target info)))))))
599 ;; Case 2: Creation of an appendix is requested.
600 ((equal environment "appendix")
601 (concat "\\appendix"
602 (org-element-property :BEAMER_ACT headline)
603 "\n"
604 (make-string (org-element-property :pre-blank headline) ?\n)
605 contents))
606 ;; Case 3: Ignore heading.
607 ((equal environment "ignoreheading")
608 (concat (make-string (org-element-property :pre-blank headline) ?\n)
609 contents))
610 ;; Case 4: HEADLINE is a note.
611 ((member environment '("note" "noteNH"))
612 (format "\\note{%s}"
613 (concat (and (equal environment "note")
614 (concat
615 (org-export-data
616 (org-element-property :title headline) info)
617 "\n"))
618 (org-trim contents))))
619 ;; Case 5: HEADLINE is a frame.
620 ((= level frame-level)
621 (org-beamer--format-frame headline contents info))
622 ;; Case 6: Regular section, extracted from
623 ;; `org-latex-classes'.
624 ((< level frame-level)
625 (org-beamer--format-section headline contents info))
626 ;; Case 7: Otherwise, HEADLINE is a block.
627 (t (org-beamer--format-block headline contents info))))))
630 ;;;; Item
632 (defun org-beamer-item (item contents info)
633 "Transcode an ITEM element into Beamer code.
634 CONTENTS holds the contents of the item. INFO is a plist holding
635 contextual information."
636 (let ((action (let ((first-element (car (org-element-contents item))))
637 (and (eq (org-element-type first-element) 'paragraph)
638 (org-beamer--element-has-overlay-p first-element))))
639 (output (org-export-with-backend 'latex item contents info)))
640 (if (or (not action) (not (string-match "\\\\item" output))) output
641 ;; If the item starts with a paragraph and that paragraph starts
642 ;; with an export snippet specifying an overlay, insert it after
643 ;; \item command.
644 (replace-match (concat "\\\\item" action) nil nil output))))
647 ;;;; Keyword
649 (defun org-beamer-keyword (keyword contents info)
650 "Transcode a KEYWORD element into Beamer code.
651 CONTENTS is nil. INFO is a plist used as a communication
652 channel."
653 (let ((key (org-element-property :key keyword))
654 (value (org-element-property :value keyword)))
655 ;; Handle specifically BEAMER and TOC (headlines only) keywords.
656 ;; Otherwise, fallback to `latex' back-end.
657 (cond
658 ((equal key "BEAMER") value)
659 ((and (equal key "TOC") (string-match "\\<headlines\\>" value))
660 (let ((depth (or (and (string-match "[0-9]+" value)
661 (string-to-number (match-string 0 value)))
662 (plist-get info :with-toc)))
663 (options (and (string-match "\\[.*?\\]" value)
664 (match-string 0 value))))
665 (concat
666 (when (wholenump depth) (format "\\setcounter{tocdepth}{%s}\n" depth))
667 "\\tableofcontents" options)))
668 (t (org-export-with-backend 'latex keyword contents info)))))
671 ;;;; Link
673 (defun org-beamer-link (link contents info)
674 "Transcode a LINK object into Beamer code.
675 CONTENTS is the description part of the link. INFO is a plist
676 used as a communication channel."
677 (let ((type (org-element-property :type link))
678 (path (org-element-property :path link)))
679 ;; Use \hyperlink command for all internal links.
680 (cond
681 ((equal type "radio")
682 (let ((destination (org-export-resolve-radio-link link info)))
683 (when destination
684 (format "\\hyperlink%s{%s}{%s}"
685 (or (org-beamer--element-has-overlay-p link) "")
686 (org-export-solidify-link-text
687 (org-element-property :value destination))
688 contents))))
689 ((and (member type '("custom-id" "fuzzy" "id"))
690 (let ((destination (if (string= type "fuzzy")
691 (org-export-resolve-fuzzy-link link info)
692 (org-export-resolve-id-link link info))))
693 (case (org-element-type destination)
694 (headline
695 (let ((label
696 (format "sec-%s"
697 (mapconcat
698 'number-to-string
699 (org-export-get-headline-number
700 destination info)
701 "-"))))
702 (if (and (plist-get info :section-numbers) (not contents))
703 (format "\\ref{%s}" label)
704 (format "\\hyperlink%s{%s}{%s}"
705 (or (org-beamer--element-has-overlay-p link) "")
706 label
707 contents))))
708 (target
709 (let ((path (org-export-solidify-link-text path)))
710 (if (not contents) (format "\\ref{%s}" path)
711 (format "\\hyperlink%s{%s}{%s}"
712 (or (org-beamer--element-has-overlay-p link) "")
713 path
714 contents))))))))
715 ;; Otherwise, use `latex' back-end.
716 (t (org-export-with-backend 'latex link contents info)))))
719 ;;;; Plain List
721 ;; Plain lists support `:environment', `:overlay' and `:options'
722 ;; attributes.
724 (defun org-beamer-plain-list (plain-list contents info)
725 "Transcode a PLAIN-LIST element into Beamer code.
726 CONTENTS is the contents of the list. INFO is a plist holding
727 contextual information."
728 (let* ((type (org-element-property :type plain-list))
729 (attributes (org-combine-plists
730 (org-export-read-attribute :attr_latex plain-list)
731 (org-export-read-attribute :attr_beamer plain-list)))
732 (latex-type (let ((env (plist-get attributes :environment)))
733 (cond (env)
734 ((eq type 'ordered) "enumerate")
735 ((eq type 'descriptive) "description")
736 (t "itemize")))))
737 (org-latex--wrap-label
738 plain-list
739 (format "\\begin{%s}%s%s\n%s\\end{%s}"
740 latex-type
741 ;; Default overlay specification, if any.
742 (org-beamer--normalize-argument
743 (or (plist-get attributes :overlay) "")
744 'defaction)
745 ;; Second optional argument depends on the list type.
746 (org-beamer--normalize-argument
747 (or (plist-get attributes :options) "")
748 'option)
749 ;; Eventually insert contents and close environment.
750 contents
751 latex-type))))
754 ;;;; Radio Target
756 (defun org-beamer-radio-target (radio-target text info)
757 "Transcode a RADIO-TARGET object into Beamer code.
758 TEXT is the text of the target. INFO is a plist holding
759 contextual information."
760 (format "\\hypertarget%s{%s}{%s}"
761 (or (org-beamer--element-has-overlay-p radio-target) "")
762 (org-export-solidify-link-text
763 (org-element-property :value radio-target))
764 text))
767 ;;;; Target
769 (defun org-beamer-target (target contents info)
770 "Transcode a TARGET object into Beamer code.
771 CONTENTS is nil. INFO is a plist holding contextual
772 information."
773 (format "\\hypertarget{%s}{}"
774 (org-export-solidify-link-text (org-element-property :value target))))
777 ;;;; Template
779 ;; Template used is similar to the one used in `latex' back-end,
780 ;; excepted for the table of contents and Beamer themes.
782 (defun org-beamer-template (contents info)
783 "Return complete document string after Beamer conversion.
784 CONTENTS is the transcoded contents string. INFO is a plist
785 holding export options."
786 (let ((title (org-export-data (plist-get info :title) info)))
787 (concat
788 ;; 1. Time-stamp.
789 (and (plist-get info :time-stamp-file)
790 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
791 ;; 2. Document class and packages.
792 (let* ((class (plist-get info :latex-class))
793 (class-options (plist-get info :latex-class-options))
794 (header (nth 1 (assoc class org-latex-classes)))
795 (document-class-string
796 (and (stringp header)
797 (if (not class-options) header
798 (replace-regexp-in-string
799 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
800 class-options header t nil 1)))))
801 (if (not document-class-string)
802 (user-error "Unknown LaTeX class `%s'" class)
803 (org-latex-guess-babel-language
804 (org-latex-guess-inputenc
805 (org-element-normalize-string
806 (org-splice-latex-header
807 document-class-string
808 org-latex-default-packages-alist
809 org-latex-packages-alist nil
810 (concat (org-element-normalize-string
811 (plist-get info :latex-header))
812 (org-element-normalize-string
813 (plist-get info :latex-header-extra))
814 (plist-get info :beamer-header-extra)))))
815 info)))
816 ;; 3. Insert themes.
817 (let ((format-theme
818 (function
819 (lambda (prop command)
820 (let ((theme (plist-get info prop)))
821 (when theme
822 (concat command
823 (if (not (string-match "\\[.*\\]" theme))
824 (format "{%s}\n" theme)
825 (format "%s{%s}\n"
826 (match-string 0 theme)
827 (org-trim
828 (replace-match "" nil nil theme)))))))))))
829 (mapconcat (lambda (args) (apply format-theme args))
830 '((:beamer-theme "\\usetheme")
831 (:beamer-color-theme "\\usecolortheme")
832 (:beamer-font-theme "\\usefonttheme")
833 (:beamer-inner-theme "\\useinnertheme")
834 (:beamer-outer-theme "\\useoutertheme"))
835 ""))
836 ;; 4. Possibly limit depth for headline numbering.
837 (let ((sec-num (plist-get info :section-numbers)))
838 (when (integerp sec-num)
839 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
840 ;; 5. Author.
841 (let ((author (and (plist-get info :with-author)
842 (let ((auth (plist-get info :author)))
843 (and auth (org-export-data auth info)))))
844 (email (and (plist-get info :with-email)
845 (org-export-data (plist-get info :email) info))))
846 (cond ((and author email (not (string= "" email)))
847 (format "\\author{%s\\thanks{%s}}\n" author email))
848 (author (format "\\author{%s}\n" author))
849 (t "\\author{}\n")))
850 ;; 6. Date.
851 (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
852 (format "\\date{%s}\n" (org-export-data date info)))
853 ;; 7. Title
854 (format "\\title{%s}\n" title)
855 ;; 8. Hyperref options.
856 (when (plist-get info :latex-hyperref-p)
857 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
858 (or (plist-get info :keywords) "")
859 (or (plist-get info :description) "")
860 (if (not (plist-get info :with-creator)) ""
861 (plist-get info :creator))))
862 ;; 9. Document start.
863 "\\begin{document}\n\n"
864 ;; 10. Title command.
865 (org-element-normalize-string
866 (cond ((string= "" title) nil)
867 ((not (stringp org-latex-title-command)) nil)
868 ((string-match "\\(?:[^%]\\|^\\)%s"
869 org-latex-title-command)
870 (format org-latex-title-command title))
871 (t org-latex-title-command)))
872 ;; 11. Table of contents.
873 (let ((depth (plist-get info :with-toc)))
874 (when depth
875 (concat
876 (format "\\begin{frame}%s{%s}\n"
877 (org-beamer--normalize-argument
878 org-beamer-outline-frame-options 'option)
879 org-beamer-outline-frame-title)
880 (when (wholenump depth)
881 (format "\\setcounter{tocdepth}{%d}\n" depth))
882 "\\tableofcontents\n"
883 "\\end{frame}\n\n")))
884 ;; 12. Document's body.
885 contents
886 ;; 13. Creator.
887 (let ((creator-info (plist-get info :with-creator)))
888 (cond
889 ((not creator-info) "")
890 ((eq creator-info 'comment)
891 (format "%% %s\n" (plist-get info :creator)))
892 (t (concat (plist-get info :creator) "\n"))))
893 ;; 14. Document end.
894 "\\end{document}")))
898 ;;; Minor Mode
901 (defvar org-beamer-mode-map (make-sparse-keymap)
902 "The keymap for `org-beamer-mode'.")
903 (define-key org-beamer-mode-map "\C-c\C-b" 'org-beamer-select-environment)
905 ;;;###autoload
906 (define-minor-mode org-beamer-mode
907 "Support for editing Beamer oriented Org mode files."
908 nil " Bm" 'org-beamer-mode-map)
910 (when (fboundp 'font-lock-add-keywords)
911 (font-lock-add-keywords
912 'org-mode
913 '((":\\(B_[a-z]+\\|BMCOL\\):" 1 'org-beamer-tag prepend))
914 'prepend))
916 (defface org-beamer-tag '((t (:box (:line-width 1 :color grey40))))
917 "The special face for beamer tags."
918 :group 'org-export-beamer)
920 (defun org-beamer-property-changed (property value)
921 "Track the BEAMER_env property with tags.
922 PROPERTY is the name of the modified property. VALUE is its new
923 value."
924 (cond
925 ((equal property "BEAMER_env")
926 (save-excursion
927 (org-back-to-heading t)
928 ;; Filter out Beamer-related tags and install environment tag.
929 (let ((tags (org-remove-if (lambda (x) (string-match "^B_" x))
930 (org-get-tags)))
931 (env-tag (and (org-string-nw-p value) (concat "B_" value))))
932 (org-set-tags-to (if env-tag (cons env-tag tags) tags))
933 (when env-tag (org-toggle-tag env-tag 'on)))))
934 ((equal property "BEAMER_col")
935 (org-toggle-tag "BMCOL" (if (org-string-nw-p value) 'on 'off)))))
937 (add-hook 'org-property-changed-functions 'org-beamer-property-changed)
939 (defun org-beamer-allowed-property-values (property)
940 "Supply allowed values for PROPERTY."
941 (cond
942 ((and (equal property "BEAMER_env")
943 (not (org-entry-get nil (concat property "_ALL") 'inherit)))
944 ;; If no allowed values for BEAMER_env have been defined,
945 ;; supply all defined environments
946 (mapcar 'car (append org-beamer-environments-special
947 org-beamer-environments-extra
948 org-beamer-environments-default)))
949 ((and (equal property "BEAMER_col")
950 (not (org-entry-get nil (concat property "_ALL") 'inherit)))
951 ;; If no allowed values for BEAMER_col have been defined,
952 ;; supply some
953 (org-split-string org-beamer-column-widths " "))))
955 (add-hook 'org-property-allowed-value-functions
956 'org-beamer-allowed-property-values)
960 ;;; Commands
962 ;;;###autoload
963 (defun org-beamer-export-as-latex
964 (&optional async subtreep visible-only body-only ext-plist)
965 "Export current buffer as a Beamer buffer.
967 If narrowing is active in the current buffer, only export its
968 narrowed part.
970 If a region is active, export that region.
972 A non-nil optional argument ASYNC means the process should happen
973 asynchronously. The resulting buffer should be accessible
974 through the `org-export-stack' interface.
976 When optional argument SUBTREEP is non-nil, export the sub-tree
977 at point, extracting information from the headline properties
978 first.
980 When optional argument VISIBLE-ONLY is non-nil, don't export
981 contents of hidden elements.
983 When optional argument BODY-ONLY is non-nil, only write code
984 between \"\\begin{document}\" and \"\\end{document}\".
986 EXT-PLIST, when provided, is a property list with external
987 parameters overriding Org default settings, but still inferior to
988 file-local settings.
990 Export is done in a buffer named \"*Org BEAMER Export*\", which
991 will be displayed when `org-export-show-temporary-export-buffer'
992 is non-nil."
993 (interactive)
994 (org-export-to-buffer 'beamer "*Org BEAMER Export*"
995 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
997 ;;;###autoload
998 (defun org-beamer-export-to-latex
999 (&optional async subtreep visible-only body-only ext-plist)
1000 "Export current buffer as a Beamer presentation (tex).
1002 If narrowing is active in the current buffer, only export its
1003 narrowed part.
1005 If a region is active, export that region.
1007 A non-nil optional argument ASYNC means the process should happen
1008 asynchronously. The resulting file should be accessible through
1009 the `org-export-stack' interface.
1011 When optional argument SUBTREEP is non-nil, export the sub-tree
1012 at point, extracting information from the headline properties
1013 first.
1015 When optional argument VISIBLE-ONLY is non-nil, don't export
1016 contents of hidden elements.
1018 When optional argument BODY-ONLY is non-nil, only write code
1019 between \"\\begin{document}\" and \"\\end{document}\".
1021 EXT-PLIST, when provided, is a property list with external
1022 parameters overriding Org default settings, but still inferior to
1023 file-local settings.
1025 Return output file's name."
1026 (interactive)
1027 (let ((file (org-export-output-file-name ".tex" subtreep)))
1028 (org-export-to-file 'beamer file
1029 async subtreep visible-only body-only ext-plist)))
1031 ;;;###autoload
1032 (defun org-beamer-export-to-pdf
1033 (&optional async subtreep visible-only body-only ext-plist)
1034 "Export current buffer as a Beamer presentation (PDF).
1036 If narrowing is active in the current buffer, only export its
1037 narrowed part.
1039 If a region is active, export that region.
1041 A non-nil optional argument ASYNC means the process should happen
1042 asynchronously. The resulting file should be accessible through
1043 the `org-export-stack' interface.
1045 When optional argument SUBTREEP is non-nil, export the sub-tree
1046 at point, extracting information from the headline properties
1047 first.
1049 When optional argument VISIBLE-ONLY is non-nil, don't export
1050 contents of hidden elements.
1052 When optional argument BODY-ONLY is non-nil, only write code
1053 between \"\\begin{document}\" and \"\\end{document}\".
1055 EXT-PLIST, when provided, is a property list with external
1056 parameters overriding Org default settings, but still inferior to
1057 file-local settings.
1059 Return PDF file's name."
1060 (interactive)
1061 (let ((file (org-export-output-file-name ".tex" subtreep)))
1062 (org-export-to-file 'beamer file
1063 async subtreep visible-only body-only ext-plist
1064 (lambda (file) (org-latex-compile file)))))
1066 ;;;###autoload
1067 (defun org-beamer-select-environment ()
1068 "Select the environment to be used by beamer for this entry.
1069 While this uses (for convenience) a tag selection interface, the
1070 result of this command will be that the BEAMER_env *property* of
1071 the entry is set.
1073 In addition to this, the command will also set a tag as a visual
1074 aid, but the tag does not have any semantic meaning."
1075 (interactive)
1076 ;; Make sure `org-beamer-environments-special' has a higher
1077 ;; priority than `org-beamer-environments-extra'.
1078 (let* ((envs (append org-beamer-environments-special
1079 org-beamer-environments-extra
1080 org-beamer-environments-default))
1081 (org-tag-alist
1082 (append '((:startgroup))
1083 (mapcar (lambda (e) (cons (concat "B_" (car e))
1084 (string-to-char (nth 1 e))))
1085 envs)
1086 '((:endgroup))
1087 '(("BMCOL" . ?|))))
1088 (org-tag-persistent-alist nil)
1089 (org-use-fast-tag-selection t)
1090 (org-fast-tag-selection-single-key t))
1091 (org-set-tags)
1092 (let ((tags (or (ignore-errors (org-get-tags-string)) "")))
1093 (cond
1094 ;; For a column, automatically ask for its width.
1095 ((eq org-last-tag-selection-key ?|)
1096 (if (string-match ":BMCOL:" tags)
1097 (org-set-property "BEAMER_col" (read-string "Column width: "))
1098 (org-delete-property "BEAMER_col")))
1099 ;; For an "againframe" section, automatically ask for reference
1100 ;; to resumed frame and overlay specifications.
1101 ((eq org-last-tag-selection-key ?A)
1102 (if (equal (org-entry-get nil "BEAMER_env") "againframe")
1103 (progn (org-entry-delete nil "BEAMER_env")
1104 (org-entry-delete nil "BEAMER_ref")
1105 (org-entry-delete nil "BEAMER_act"))
1106 (org-entry-put nil "BEAMER_env" "againframe")
1107 (org-set-property
1108 "BEAMER_ref"
1109 (read-string "Frame reference (*Title, #custom-id, id:...): "))
1110 (org-set-property "BEAMER_act"
1111 (read-string "Overlay specification: "))))
1112 ((string-match (concat ":B_\\(" (mapconcat 'car envs "\\|") "\\):") tags)
1113 (org-entry-put nil "BEAMER_env" (match-string 1 tags)))
1114 (t (org-entry-delete nil "BEAMER_env"))))))
1116 ;;;###autoload
1117 (defun org-beamer-publish-to-latex (plist filename pub-dir)
1118 "Publish an Org file to a Beamer presentation (LaTeX).
1120 FILENAME is the filename of the Org file to be published. PLIST
1121 is the property list for the given project. PUB-DIR is the
1122 publishing directory.
1124 Return output file name."
1125 (org-publish-org-to 'beamer filename ".tex" plist pub-dir))
1127 ;;;###autoload
1128 (defun org-beamer-publish-to-pdf (plist filename pub-dir)
1129 "Publish an Org file to a Beamer presentation (PDF, via LaTeX).
1131 FILENAME is the filename of the Org file to be published. PLIST
1132 is the property list for the given project. PUB-DIR is the
1133 publishing directory.
1135 Return output file name."
1136 ;; Unlike to `org-beamer-publish-to-latex', PDF file is generated in
1137 ;; working directory and then moved to publishing directory.
1138 (org-publish-attachment
1139 plist
1140 (org-latex-compile
1141 (org-publish-org-to
1142 'beamer filename ".tex" plist (file-name-directory filename)))
1143 pub-dir))
1146 (provide 'ox-beamer)
1148 ;; Local variables:
1149 ;; generated-autoload-file: "org-loaddefs.el"
1150 ;; End:
1152 ;;; ox-beamer.el ends here