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