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