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