Use options instead of hard-coded variables
[org-mode.git] / lisp / ox-beamer.el
blob97763e94b49b41d191e737924b36d7873732e379
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
410 (plist-get info :beamer-frame-default-options) ",")
411 (and beamer-opt
412 (org-split-string
413 ;; Remove square brackets if user provided
414 ;; them.
415 (and (string-match "^\\[?\\(.*\\)\\]?$" beamer-opt)
416 (match-string 1 beamer-opt))
417 ","))
418 ;; Provide an automatic label for the frame
419 ;; unless the user specified one.
420 (unless (and beamer-opt
421 (string-match "\\(^\\|,\\)label=" beamer-opt))
422 (list
423 (format "label=%s"
424 (org-beamer--get-label headline info)))))))
425 ;; Change options list into a string.
426 (org-beamer--normalize-argument
427 (mapconcat
428 'identity
429 (if (or (not fragilep) (member "fragile" options)) options
430 (cons "fragile" options))
431 ",")
432 'option))
433 ;; Title.
434 (let ((env (org-element-property :BEAMER_ENV headline)))
435 (format "{%s}"
436 (if (and env (equal (downcase env) "fullframe")) ""
437 (org-export-data
438 (org-element-property :title headline) info))))
439 "\n"
440 ;; The following workaround is required in fragile frames
441 ;; as Beamer will append "\par" to the beginning of the
442 ;; contents. So we need to make sure the command is
443 ;; separated from the contents by at least one space. If
444 ;; it isn't, it will create "\parfirst-word" command and
445 ;; remove the first word from the contents in the PDF
446 ;; output.
447 (if (not fragilep) contents
448 (replace-regexp-in-string "\\`\n*" "\\& " (or contents "")))
449 "\\end{frame}")))
451 (defun org-beamer--format-block (headline contents info)
452 "Format HEADLINE as a block.
453 CONTENTS holds the contents of the headline. INFO is a plist
454 used as a communication channel."
455 (let* ((column-width (org-element-property :BEAMER_COL headline))
456 ;; ENVIRONMENT defaults to "block" if none is specified and
457 ;; there is no column specification. If there is a column
458 ;; specified but still no explicit environment, ENVIRONMENT
459 ;; is "column".
460 (environment (let ((env (org-element-property :BEAMER_ENV headline)))
461 (cond
462 ;; "block" is the fallback environment.
463 ((and (not env) (not column-width)) "block")
464 ;; "column" only.
465 ((not env) "column")
466 ;; Use specified environment.
467 (t env))))
468 (raw-title (org-element-property :raw-value headline))
469 (env-format
470 (cond ((member environment '("column" "columns")) nil)
471 ((assoc environment
472 (append (plist-get info :beamer-environments-extra)
473 org-beamer-environments-default)))
474 (t (user-error "Wrong block type at a headline named \"%s\""
475 raw-title))))
476 (title (org-export-data (org-element-property :title headline) info))
477 (options (let ((options (org-element-property :BEAMER_OPT headline)))
478 (if (not options) ""
479 (org-beamer--normalize-argument options 'option))))
480 ;; Start a "columns" environment when explicitly requested or
481 ;; when there is no previous headline or the previous
482 ;; headline do not have a BEAMER_column property.
483 (parent-env (org-element-property
484 :BEAMER_ENV (org-export-get-parent-headline headline)))
485 (start-columns-p
486 (or (equal environment "columns")
487 (and column-width
488 (not (and parent-env
489 (equal (downcase parent-env) "columns")))
490 (or (org-export-first-sibling-p headline info)
491 (not (org-element-property
492 :BEAMER_COL
493 (org-export-get-previous-element
494 headline info)))))))
495 ;; End the "columns" environment when explicitly requested or
496 ;; when there is no next headline or the next headline do not
497 ;; have a BEAMER_column property.
498 (end-columns-p
499 (or (equal environment "columns")
500 (and column-width
501 (not (and parent-env
502 (equal (downcase parent-env) "columns")))
503 (or (org-export-last-sibling-p headline info)
504 (not (org-element-property
505 :BEAMER_COL
506 (org-export-get-next-element headline info))))))))
507 (concat
508 (when start-columns-p
509 ;; Column can accept options only when the environment is
510 ;; explicitly defined.
511 (if (not (equal environment "columns")) "\\begin{columns}\n"
512 (format "\\begin{columns}%s\n" options)))
513 (when column-width
514 (format "\\begin{column}%s{%s}\n"
515 ;; One can specify placement for column only when
516 ;; HEADLINE stands for a column on its own.
517 (if (equal environment "column") options "")
518 (format "%s\\textwidth" column-width)))
519 ;; Block's opening string.
520 (when (nth 2 env-format)
521 (concat
522 (org-fill-template
523 (nth 2 env-format)
524 (nconc
525 ;; If BEAMER_act property has its value enclosed in square
526 ;; brackets, it is a default overlay specification and
527 ;; overlay specification is empty. Otherwise, it is an
528 ;; overlay specification and the default one is nil.
529 (let ((action (org-element-property :BEAMER_ACT headline)))
530 (cond
531 ((not action) (list (cons "a" "") (cons "A" "")))
532 ((string-match "\\`\\[.*\\]\\'" action)
533 (list
534 (cons "A" (org-beamer--normalize-argument action 'defaction))
535 (cons "a" "")))
537 (list (cons "a" (org-beamer--normalize-argument action 'action))
538 (cons "A" "")))))
539 (list (cons "o" options)
540 (cons "h" title)
541 (cons "r" raw-title)
542 (cons "H" (if (equal raw-title "") ""
543 (format "{%s}" raw-title)))
544 (cons "U" (if (equal raw-title "") ""
545 (format "[%s]" raw-title))))))
546 "\n"))
547 contents
548 ;; Block's closing string, if any.
549 (and (nth 3 env-format) (concat (nth 3 env-format) "\n"))
550 (when column-width "\\end{column}\n")
551 (when end-columns-p "\\end{columns}"))))
553 (defun org-beamer-headline (headline contents info)
554 "Transcode HEADLINE element into Beamer code.
555 CONTENTS is the contents of the headline. INFO is a plist used
556 as a communication channel."
557 (unless (org-element-property :footnote-section-p headline)
558 (let ((level (org-export-get-relative-level headline info))
559 (frame-level (org-beamer--frame-level headline info))
560 (environment (let ((env (org-element-property :BEAMER_ENV headline)))
561 (or (org-string-nw-p env) "block"))))
562 (cond
563 ;; Case 1: Resume frame specified by "BEAMER_ref" property.
564 ((equal environment "againframe")
565 (let ((ref (org-element-property :BEAMER_REF headline)))
566 ;; Reference to frame being resumed is mandatory. Ignore
567 ;; the whole headline if it isn't provided.
568 (when (org-string-nw-p ref)
569 (concat "\\againframe"
570 ;; Overlay specification.
571 (let ((overlay (org-element-property :BEAMER_ACT headline)))
572 (when overlay
573 (org-beamer--normalize-argument
574 overlay
575 (if (string-match "^\\[.*\\]$" overlay) 'defaction
576 'action))))
577 ;; Options.
578 (let ((options (org-element-property :BEAMER_OPT headline)))
579 (when options
580 (org-beamer--normalize-argument options 'option)))
581 ;; Resolve reference provided by "BEAMER_ref"
582 ;; property. This is done by building a minimal fake
583 ;; link and calling the appropriate resolve function,
584 ;; depending on the reference syntax.
585 (let* ((type
586 (progn
587 (string-match "^\\(id:\\|#\\|\\*\\)?\\(.*\\)" ref)
588 (cond
589 ((or (not (match-string 1 ref))
590 (equal (match-string 1 ref) "*")) 'fuzzy)
591 ((equal (match-string 1 ref) "id:") 'id)
592 (t 'custom-id))))
593 (link (list 'link (list :path (match-string 2 ref))))
594 (target (if (eq type 'fuzzy)
595 (org-export-resolve-fuzzy-link link info)
596 (org-export-resolve-id-link link info))))
597 ;; Now use user-defined label provided in TARGET
598 ;; headline, or fallback to standard one.
599 (format "{%s}" (org-beamer--get-label target info)))))))
600 ;; Case 2: Creation of an appendix is requested.
601 ((equal environment "appendix")
602 (concat "\\appendix"
603 (org-element-property :BEAMER_ACT headline)
604 "\n"
605 (make-string (org-element-property :pre-blank headline) ?\n)
606 contents))
607 ;; Case 3: Ignore heading.
608 ((equal environment "ignoreheading")
609 (concat (make-string (org-element-property :pre-blank headline) ?\n)
610 contents))
611 ;; Case 4: HEADLINE is a note.
612 ((member environment '("note" "noteNH"))
613 (format "\\note{%s}"
614 (concat (and (equal environment "note")
615 (concat
616 (org-export-data
617 (org-element-property :title headline) info)
618 "\n"))
619 (org-trim contents))))
620 ;; Case 5: HEADLINE is a frame.
621 ((= level frame-level)
622 (org-beamer--format-frame headline contents info))
623 ;; Case 6: Regular section, extracted from
624 ;; `org-latex-classes'.
625 ((< level frame-level)
626 (org-beamer--format-section headline contents info))
627 ;; Case 7: Otherwise, HEADLINE is a block.
628 (t (org-beamer--format-block headline contents info))))))
631 ;;;; Item
633 (defun org-beamer-item (item contents info)
634 "Transcode an ITEM element into Beamer code.
635 CONTENTS holds the contents of the item. INFO is a plist holding
636 contextual information."
637 (let ((action (let ((first-element (car (org-element-contents item))))
638 (and (eq (org-element-type first-element) 'paragraph)
639 (org-beamer--element-has-overlay-p first-element))))
640 (output (org-export-with-backend 'latex item contents info)))
641 (if (or (not action) (not (string-match "\\\\item" output))) output
642 ;; If the item starts with a paragraph and that paragraph starts
643 ;; with an export snippet specifying an overlay, insert it after
644 ;; \item command.
645 (replace-match (concat "\\\\item" action) nil nil output))))
648 ;;;; Keyword
650 (defun org-beamer-keyword (keyword contents info)
651 "Transcode a KEYWORD element into Beamer code.
652 CONTENTS is nil. INFO is a plist used as a communication
653 channel."
654 (let ((key (org-element-property :key keyword))
655 (value (org-element-property :value keyword)))
656 ;; Handle specifically BEAMER and TOC (headlines only) keywords.
657 ;; Otherwise, fallback to `latex' back-end.
658 (cond
659 ((equal key "BEAMER") value)
660 ((and (equal key "TOC") (string-match "\\<headlines\\>" value))
661 (let ((depth (or (and (string-match "[0-9]+" value)
662 (string-to-number (match-string 0 value)))
663 (plist-get info :with-toc)))
664 (options (and (string-match "\\[.*?\\]" value)
665 (match-string 0 value))))
666 (concat
667 (when (wholenump depth) (format "\\setcounter{tocdepth}{%s}\n" depth))
668 "\\tableofcontents" options)))
669 (t (org-export-with-backend 'latex keyword contents info)))))
672 ;;;; Link
674 (defun org-beamer-link (link contents info)
675 "Transcode a LINK object into Beamer code.
676 CONTENTS is the description part of the link. INFO is a plist
677 used as a communication channel."
678 (let ((type (org-element-property :type link))
679 (path (org-element-property :path link)))
680 ;; Use \hyperlink command for all internal links.
681 (cond
682 ((equal type "radio")
683 (let ((destination (org-export-resolve-radio-link link info)))
684 (when destination
685 (format "\\hyperlink%s{%s}{%s}"
686 (or (org-beamer--element-has-overlay-p link) "")
687 (org-export-solidify-link-text
688 (org-element-property :value destination))
689 contents))))
690 ((and (member type '("custom-id" "fuzzy" "id"))
691 (let ((destination (if (string= type "fuzzy")
692 (org-export-resolve-fuzzy-link link info)
693 (org-export-resolve-id-link link info))))
694 (case (org-element-type destination)
695 (headline
696 (let ((label
697 (format "sec-%s"
698 (mapconcat
699 'number-to-string
700 (org-export-get-headline-number
701 destination info)
702 "-"))))
703 (if (and (plist-get info :section-numbers) (not contents))
704 (format "\\ref{%s}" label)
705 (format "\\hyperlink%s{%s}{%s}"
706 (or (org-beamer--element-has-overlay-p link) "")
707 label
708 contents))))
709 (target
710 (let ((path (org-export-solidify-link-text path)))
711 (if (not contents) (format "\\ref{%s}" path)
712 (format "\\hyperlink%s{%s}{%s}"
713 (or (org-beamer--element-has-overlay-p link) "")
714 path
715 contents))))))))
716 ;; Otherwise, use `latex' back-end.
717 (t (org-export-with-backend 'latex link contents info)))))
720 ;;;; Plain List
722 ;; Plain lists support `:environment', `:overlay' and `:options'
723 ;; attributes.
725 (defun org-beamer-plain-list (plain-list contents info)
726 "Transcode a PLAIN-LIST element into Beamer code.
727 CONTENTS is the contents of the list. INFO is a plist holding
728 contextual information."
729 (let* ((type (org-element-property :type plain-list))
730 (attributes (org-combine-plists
731 (org-export-read-attribute :attr_latex plain-list)
732 (org-export-read-attribute :attr_beamer plain-list)))
733 (latex-type (let ((env (plist-get attributes :environment)))
734 (cond (env)
735 ((eq type 'ordered) "enumerate")
736 ((eq type 'descriptive) "description")
737 (t "itemize")))))
738 (org-latex--wrap-label
739 plain-list
740 (format "\\begin{%s}%s%s\n%s\\end{%s}"
741 latex-type
742 ;; Default overlay specification, if any.
743 (org-beamer--normalize-argument
744 (or (plist-get attributes :overlay) "")
745 'defaction)
746 ;; Second optional argument depends on the list type.
747 (org-beamer--normalize-argument
748 (or (plist-get attributes :options) "")
749 'option)
750 ;; Eventually insert contents and close environment.
751 contents
752 latex-type))))
755 ;;;; Radio Target
757 (defun org-beamer-radio-target (radio-target text info)
758 "Transcode a RADIO-TARGET object into Beamer code.
759 TEXT is the text of the target. INFO is a plist holding
760 contextual information."
761 (format "\\hypertarget%s{%s}{%s}"
762 (or (org-beamer--element-has-overlay-p radio-target) "")
763 (org-export-solidify-link-text
764 (org-element-property :value radio-target))
765 text))
768 ;;;; Target
770 (defun org-beamer-target (target contents info)
771 "Transcode a TARGET object into Beamer code.
772 CONTENTS is nil. INFO is a plist holding contextual
773 information."
774 (format "\\hypertarget{%s}{}"
775 (org-export-solidify-link-text (org-element-property :value target))))
778 ;;;; Template
780 ;; Template used is similar to the one used in `latex' back-end,
781 ;; excepted for the table of contents and Beamer themes.
783 (defun org-beamer-template (contents info)
784 "Return complete document string after Beamer conversion.
785 CONTENTS is the transcoded contents string. INFO is a plist
786 holding export options."
787 (let ((title (org-export-data (plist-get info :title) info)))
788 (concat
789 ;; 1. Time-stamp.
790 (and (plist-get info :time-stamp-file)
791 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
792 ;; 2. Document class and packages.
793 (let* ((class (plist-get info :latex-class))
794 (class-options (plist-get info :latex-class-options))
795 (header (nth 1 (assoc class org-latex-classes)))
796 (document-class-string
797 (and (stringp header)
798 (if (not class-options) header
799 (replace-regexp-in-string
800 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
801 class-options header t nil 1)))))
802 (if (not document-class-string)
803 (user-error "Unknown LaTeX class `%s'" class)
804 (org-latex-guess-babel-language
805 (org-latex-guess-inputenc
806 (org-element-normalize-string
807 (org-splice-latex-header
808 document-class-string
809 org-latex-default-packages-alist
810 org-latex-packages-alist nil
811 (concat (org-element-normalize-string
812 (plist-get info :latex-header))
813 (org-element-normalize-string
814 (plist-get info :latex-header-extra))
815 (plist-get info :beamer-header-extra)))))
816 info)))
817 ;; 3. Insert themes.
818 (let ((format-theme
819 (function
820 (lambda (prop command)
821 (let ((theme (plist-get info prop)))
822 (when theme
823 (concat command
824 (if (not (string-match "\\[.*\\]" theme))
825 (format "{%s}\n" theme)
826 (format "%s{%s}\n"
827 (match-string 0 theme)
828 (org-trim
829 (replace-match "" nil nil theme)))))))))))
830 (mapconcat (lambda (args) (apply format-theme args))
831 '((:beamer-theme "\\usetheme")
832 (:beamer-color-theme "\\usecolortheme")
833 (:beamer-font-theme "\\usefonttheme")
834 (:beamer-inner-theme "\\useinnertheme")
835 (:beamer-outer-theme "\\useoutertheme"))
836 ""))
837 ;; 4. Possibly limit depth for headline numbering.
838 (let ((sec-num (plist-get info :section-numbers)))
839 (when (integerp sec-num)
840 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
841 ;; 5. Author.
842 (let ((author (and (plist-get info :with-author)
843 (let ((auth (plist-get info :author)))
844 (and auth (org-export-data auth info)))))
845 (email (and (plist-get info :with-email)
846 (org-export-data (plist-get info :email) info))))
847 (cond ((and author email (not (string= "" email)))
848 (format "\\author{%s\\thanks{%s}}\n" author email))
849 (author (format "\\author{%s}\n" author))
850 (t "\\author{}\n")))
851 ;; 6. Date.
852 (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
853 (format "\\date{%s}\n" (org-export-data date info)))
854 ;; 7. Title
855 (format "\\title{%s}\n" title)
856 ;; 8. Hyperref options.
857 (when (plist-get info :latex-hyperref-p)
858 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
859 (or (plist-get info :keywords) "")
860 (or (plist-get info :description) "")
861 (if (not (plist-get info :with-creator)) ""
862 (plist-get info :creator))))
863 ;; 9. Document start.
864 "\\begin{document}\n\n"
865 ;; 10. Title command.
866 (org-element-normalize-string
867 (cond ((string= "" title) nil)
868 ((not (stringp org-latex-title-command)) nil)
869 ((string-match "\\(?:[^%]\\|^\\)%s"
870 org-latex-title-command)
871 (format org-latex-title-command title))
872 (t org-latex-title-command)))
873 ;; 11. Table of contents.
874 (let ((depth (plist-get info :with-toc)))
875 (when depth
876 (concat
877 (format "\\begin{frame}%s{%s}\n"
878 (org-beamer--normalize-argument
879 (plist-get info :beamer-outline-frame-options) 'option)
880 (plist-get info :beamer-outline-frame-title))
881 (when (wholenump depth)
882 (format "\\setcounter{tocdepth}{%d}\n" depth))
883 "\\tableofcontents\n"
884 "\\end{frame}\n\n")))
885 ;; 12. Document's body.
886 contents
887 ;; 13. Creator.
888 (let ((creator-info (plist-get info :with-creator)))
889 (cond
890 ((not creator-info) "")
891 ((eq creator-info 'comment)
892 (format "%% %s\n" (plist-get info :creator)))
893 (t (concat (plist-get info :creator) "\n"))))
894 ;; 14. Document end.
895 "\\end{document}")))
899 ;;; Minor Mode
902 (defvar org-beamer-mode-map (make-sparse-keymap)
903 "The keymap for `org-beamer-mode'.")
904 (define-key org-beamer-mode-map "\C-c\C-b" 'org-beamer-select-environment)
906 ;;;###autoload
907 (define-minor-mode org-beamer-mode
908 "Support for editing Beamer oriented Org mode files."
909 nil " Bm" 'org-beamer-mode-map)
911 (when (fboundp 'font-lock-add-keywords)
912 (font-lock-add-keywords
913 'org-mode
914 '((":\\(B_[a-z]+\\|BMCOL\\):" 1 'org-beamer-tag prepend))
915 'prepend))
917 (defface org-beamer-tag '((t (:box (:line-width 1 :color grey40))))
918 "The special face for beamer tags."
919 :group 'org-export-beamer)
921 (defun org-beamer-property-changed (property value)
922 "Track the BEAMER_env property with tags.
923 PROPERTY is the name of the modified property. VALUE is its new
924 value."
925 (cond
926 ((equal property "BEAMER_env")
927 (save-excursion
928 (org-back-to-heading t)
929 ;; Filter out Beamer-related tags and install environment tag.
930 (let ((tags (org-remove-if (lambda (x) (string-match "^B_" x))
931 (org-get-tags)))
932 (env-tag (and (org-string-nw-p value) (concat "B_" value))))
933 (org-set-tags-to (if env-tag (cons env-tag tags) tags))
934 (when env-tag (org-toggle-tag env-tag 'on)))))
935 ((equal property "BEAMER_col")
936 (org-toggle-tag "BMCOL" (if (org-string-nw-p value) 'on 'off)))))
938 (add-hook 'org-property-changed-functions 'org-beamer-property-changed)
940 (defun org-beamer-allowed-property-values (property)
941 "Supply allowed values for PROPERTY."
942 (cond
943 ((and (equal property "BEAMER_env")
944 (not (org-entry-get nil (concat property "_ALL") 'inherit)))
945 ;; If no allowed values for BEAMER_env have been defined,
946 ;; supply all defined environments
947 (mapcar 'car (append org-beamer-environments-special
948 org-beamer-environments-extra
949 org-beamer-environments-default)))
950 ((and (equal property "BEAMER_col")
951 (not (org-entry-get nil (concat property "_ALL") 'inherit)))
952 ;; If no allowed values for BEAMER_col have been defined,
953 ;; supply some
954 (org-split-string org-beamer-column-widths " "))))
956 (add-hook 'org-property-allowed-value-functions
957 'org-beamer-allowed-property-values)
961 ;;; Commands
963 ;;;###autoload
964 (defun org-beamer-export-as-latex
965 (&optional async subtreep visible-only body-only ext-plist)
966 "Export current buffer as a Beamer buffer.
968 If narrowing is active in the current buffer, only export its
969 narrowed part.
971 If a region is active, export that region.
973 A non-nil optional argument ASYNC means the process should happen
974 asynchronously. The resulting buffer should be accessible
975 through the `org-export-stack' interface.
977 When optional argument SUBTREEP is non-nil, export the sub-tree
978 at point, extracting information from the headline properties
979 first.
981 When optional argument VISIBLE-ONLY is non-nil, don't export
982 contents of hidden elements.
984 When optional argument BODY-ONLY is non-nil, only write code
985 between \"\\begin{document}\" and \"\\end{document}\".
987 EXT-PLIST, when provided, is a property list with external
988 parameters overriding Org default settings, but still inferior to
989 file-local settings.
991 Export is done in a buffer named \"*Org BEAMER Export*\", which
992 will be displayed when `org-export-show-temporary-export-buffer'
993 is non-nil."
994 (interactive)
995 (org-export-to-buffer 'beamer "*Org BEAMER Export*"
996 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
998 ;;;###autoload
999 (defun org-beamer-export-to-latex
1000 (&optional async subtreep visible-only body-only ext-plist)
1001 "Export current buffer as a Beamer presentation (tex).
1003 If narrowing is active in the current buffer, only export its
1004 narrowed part.
1006 If a region is active, export that region.
1008 A non-nil optional argument ASYNC means the process should happen
1009 asynchronously. The resulting file should be accessible through
1010 the `org-export-stack' interface.
1012 When optional argument SUBTREEP is non-nil, export the sub-tree
1013 at point, extracting information from the headline properties
1014 first.
1016 When optional argument VISIBLE-ONLY is non-nil, don't export
1017 contents of hidden elements.
1019 When optional argument BODY-ONLY is non-nil, only write code
1020 between \"\\begin{document}\" and \"\\end{document}\".
1022 EXT-PLIST, when provided, is a property list with external
1023 parameters overriding Org default settings, but still inferior to
1024 file-local settings.
1026 Return output file's name."
1027 (interactive)
1028 (let ((file (org-export-output-file-name ".tex" subtreep)))
1029 (org-export-to-file 'beamer file
1030 async subtreep visible-only body-only ext-plist)))
1032 ;;;###autoload
1033 (defun org-beamer-export-to-pdf
1034 (&optional async subtreep visible-only body-only ext-plist)
1035 "Export current buffer as a Beamer presentation (PDF).
1037 If narrowing is active in the current buffer, only export its
1038 narrowed part.
1040 If a region is active, export that region.
1042 A non-nil optional argument ASYNC means the process should happen
1043 asynchronously. The resulting file should be accessible through
1044 the `org-export-stack' interface.
1046 When optional argument SUBTREEP is non-nil, export the sub-tree
1047 at point, extracting information from the headline properties
1048 first.
1050 When optional argument VISIBLE-ONLY is non-nil, don't export
1051 contents of hidden elements.
1053 When optional argument BODY-ONLY is non-nil, only write code
1054 between \"\\begin{document}\" and \"\\end{document}\".
1056 EXT-PLIST, when provided, is a property list with external
1057 parameters overriding Org default settings, but still inferior to
1058 file-local settings.
1060 Return PDF file's name."
1061 (interactive)
1062 (let ((file (org-export-output-file-name ".tex" subtreep)))
1063 (org-export-to-file 'beamer file
1064 async subtreep visible-only body-only ext-plist
1065 (lambda (file) (org-latex-compile file)))))
1067 ;;;###autoload
1068 (defun org-beamer-select-environment ()
1069 "Select the environment to be used by beamer for this entry.
1070 While this uses (for convenience) a tag selection interface, the
1071 result of this command will be that the BEAMER_env *property* of
1072 the entry is set.
1074 In addition to this, the command will also set a tag as a visual
1075 aid, but the tag does not have any semantic meaning."
1076 (interactive)
1077 ;; Make sure `org-beamer-environments-special' has a higher
1078 ;; priority than `org-beamer-environments-extra'.
1079 (let* ((envs (append org-beamer-environments-special
1080 org-beamer-environments-extra
1081 org-beamer-environments-default))
1082 (org-tag-alist
1083 (append '((:startgroup))
1084 (mapcar (lambda (e) (cons (concat "B_" (car e))
1085 (string-to-char (nth 1 e))))
1086 envs)
1087 '((:endgroup))
1088 '(("BMCOL" . ?|))))
1089 (org-tag-persistent-alist nil)
1090 (org-use-fast-tag-selection t)
1091 (org-fast-tag-selection-single-key t))
1092 (org-set-tags)
1093 (let ((tags (or (ignore-errors (org-get-tags-string)) "")))
1094 (cond
1095 ;; For a column, automatically ask for its width.
1096 ((eq org-last-tag-selection-key ?|)
1097 (if (string-match ":BMCOL:" tags)
1098 (org-set-property "BEAMER_col" (read-string "Column width: "))
1099 (org-delete-property "BEAMER_col")))
1100 ;; For an "againframe" section, automatically ask for reference
1101 ;; to resumed frame and overlay specifications.
1102 ((eq org-last-tag-selection-key ?A)
1103 (if (equal (org-entry-get nil "BEAMER_env") "againframe")
1104 (progn (org-entry-delete nil "BEAMER_env")
1105 (org-entry-delete nil "BEAMER_ref")
1106 (org-entry-delete nil "BEAMER_act"))
1107 (org-entry-put nil "BEAMER_env" "againframe")
1108 (org-set-property
1109 "BEAMER_ref"
1110 (read-string "Frame reference (*Title, #custom-id, id:...): "))
1111 (org-set-property "BEAMER_act"
1112 (read-string "Overlay specification: "))))
1113 ((string-match (concat ":B_\\(" (mapconcat 'car envs "\\|") "\\):") tags)
1114 (org-entry-put nil "BEAMER_env" (match-string 1 tags)))
1115 (t (org-entry-delete nil "BEAMER_env"))))))
1117 ;;;###autoload
1118 (defun org-beamer-publish-to-latex (plist filename pub-dir)
1119 "Publish an Org file to a Beamer presentation (LaTeX).
1121 FILENAME is the filename of the Org file to be published. PLIST
1122 is the property list for the given project. PUB-DIR is the
1123 publishing directory.
1125 Return output file name."
1126 (org-publish-org-to 'beamer filename ".tex" plist pub-dir))
1128 ;;;###autoload
1129 (defun org-beamer-publish-to-pdf (plist filename pub-dir)
1130 "Publish an Org file to a Beamer presentation (PDF, via LaTeX).
1132 FILENAME is the filename of the Org file to be published. PLIST
1133 is the property list for the given project. PUB-DIR is the
1134 publishing directory.
1136 Return output file name."
1137 ;; Unlike to `org-beamer-publish-to-latex', PDF file is generated in
1138 ;; working directory and then moved to publishing directory.
1139 (org-publish-attachment
1140 plist
1141 (org-latex-compile
1142 (org-publish-org-to
1143 'beamer filename ".tex" plist (file-name-directory filename)))
1144 pub-dir))
1147 (provide 'ox-beamer)
1149 ;; Local variables:
1150 ;; generated-autoload-file: "org-loaddefs.el"
1151 ;; End:
1153 ;;; ox-beamer.el ends here