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