Merge branch 'maint'
[org-mode.git] / lisp / ox-beamer.el
blob3119bd47515616c57aeba75678fbfb8e59a3a793
1 ;;; ox-beamer.el --- Beamer Back-End for Org Export Engine
3 ;; Copyright (C) 2007-2015 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten.dominik AT gmail DOT com>
6 ;; Nicolas Goaziou <n.goaziou AT gmail DOT com>
7 ;; Keywords: org, wp, tex
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This library implements both a Beamer back-end, derived from the
27 ;; LaTeX one and a minor mode easing structure edition of the
28 ;; document. See Org manual for more information.
30 ;;; Code:
32 (eval-when-compile (require 'cl))
33 (require 'ox-latex)
35 ;; Install a default set-up for Beamer export.
36 (unless (assoc "beamer" org-latex-classes)
37 (add-to-list 'org-latex-classes
38 '("beamer"
39 "\\documentclass[presentation]{beamer}"
40 ("\\section{%s}" . "\\section*{%s}")
41 ("\\subsection{%s}" . "\\subsection*{%s}")
42 ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))))
46 ;;; User-Configurable Variables
48 (defgroup org-export-beamer nil
49 "Options specific for using the beamer class in LaTeX export."
50 :tag "Org Beamer"
51 :group 'org-export
52 :version "24.2")
54 (defcustom org-beamer-frame-level 1
55 "The level at which headlines become frames.
57 Headlines at a lower level will be translated into a sectioning
58 structure. At a higher level, they will be translated into
59 blocks.
61 If a headline with a \"BEAMER_env\" property set to \"frame\" is
62 found within a tree, its level locally overrides this number.
64 This variable has no effect on headlines with the \"BEAMER_env\"
65 property set to either \"ignoreheading\", \"appendix\", or
66 \"note\", which will respectively, be invisible, become an
67 appendix or a note.
69 This integer is relative to the minimal level of a headline
70 within the parse tree, defined as 1."
71 :group 'org-export-beamer
72 :type 'integer)
74 (defcustom org-beamer-frame-default-options ""
75 "Default options string to use for frames.
76 For example, it could be set to \"allowframebreaks\"."
77 :group 'org-export-beamer
78 :type '(string :tag "[options]"))
80 (defcustom org-beamer-column-view-format
81 "%45ITEM %10BEAMER_env(Env) %10BEAMER_act(Act) %4BEAMER_col(Col) %8BEAMER_opt(Opt)"
82 "Column view format that should be used to fill the template."
83 :group 'org-export-beamer
84 :version "24.4"
85 :package-version '(Org . "8.0")
86 :type '(choice
87 (const :tag "Do not insert Beamer column view format" nil)
88 (string :tag "Beamer column view format")))
90 (defcustom org-beamer-theme "default"
91 "Default theme used in Beamer presentations."
92 :group 'org-export-beamer
93 :version "24.4"
94 :package-version '(Org . "8.0")
95 :type '(choice
96 (const :tag "Do not insert a Beamer theme" nil)
97 (string :tag "Beamer theme")))
99 (defcustom org-beamer-environments-extra nil
100 "Environments triggered by tags in Beamer export.
101 Each entry has 4 elements:
103 name Name of the environment
104 key Selection key for `org-beamer-select-environment'
105 open The opening template for the environment, with the following escapes
106 %a the action/overlay specification
107 %A the default action/overlay specification
108 %R the raw BEAMER_act value
109 %o the options argument, with square brackets
110 %O the raw BEAMER_opt value
111 %h the headline text
112 %r the raw headline text (i.e. without any processing)
113 %H if there is headline text, that raw text in {} braces
114 %U if there is headline text, that raw text in [] brackets
115 close The closing string of the environment."
116 :group 'org-export-beamer
117 :version "24.4"
118 :package-version '(Org . "8.1")
119 :type '(repeat
120 (list
121 (string :tag "Environment")
122 (string :tag "Selection key")
123 (string :tag "Begin")
124 (string :tag "End"))))
126 (defcustom org-beamer-outline-frame-title "Outline"
127 "Default title of a frame containing an outline."
128 :group 'org-export-beamer
129 :type '(string :tag "Outline frame title"))
131 (defcustom org-beamer-outline-frame-options ""
132 "Outline frame options appended after \\begin{frame}.
133 You might want to put e.g. \"allowframebreaks=0.9\" here."
134 :group 'org-export-beamer
135 :type '(string :tag "Outline frame options"))
138 (defcustom org-beamer-subtitle-format "\\subtitle{%s}"
139 "Format string used for transcoded subtitle.
140 The format string should have at most one \"%s\"-expression,
141 which is replaced with the subtitle."
142 :group 'org-export-beamer
143 :version "25.1"
144 :package-version '(Org . "8.3")
145 :type '(string :tag "Format string"))
148 ;;; Internal Variables
150 (defconst org-beamer-column-widths
151 "0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 0.0 :ETC"
152 "The column widths that should be installed as allowed property values.")
154 (defconst org-beamer-environments-special
155 '(("againframe" "A")
156 ("appendix" "x")
157 ("column" "c")
158 ("columns" "C")
159 ("frame" "f")
160 ("fullframe" "F")
161 ("ignoreheading" "i")
162 ("note" "n")
163 ("noteNH" "N"))
164 "Alist of environments treated in a special way by the back-end.
165 Keys are environment names, as strings, values are bindings used
166 in `org-beamer-select-environment'. Environments listed here,
167 along with their binding, are hard coded and cannot be modified
168 through `org-beamer-environments-extra' variable.")
170 (defconst org-beamer-environments-default
171 '(("block" "b" "\\begin{block}%a{%h}" "\\end{block}")
172 ("alertblock" "a" "\\begin{alertblock}%a{%h}" "\\end{alertblock}")
173 ("verse" "v" "\\begin{verse}%a %% %h" "\\end{verse}")
174 ("quotation" "q" "\\begin{quotation}%a %% %h" "\\end{quotation}")
175 ("quote" "Q" "\\begin{quote}%a %% %h" "\\end{quote}")
176 ("structureenv" "s" "\\begin{structureenv}%a %% %h" "\\end{structureenv}")
177 ("theorem" "t" "\\begin{theorem}%a%U" "\\end{theorem}")
178 ("definition" "d" "\\begin{definition}%a%U" "\\end{definition}")
179 ("example" "e" "\\begin{example}%a%U" "\\end{example}")
180 ("exampleblock" "E" "\\begin{exampleblock}%a{%h}" "\\end{exampleblock}")
181 ("proof" "p" "\\begin{proof}%a%U" "\\end{proof}")
182 ("beamercolorbox" "o" "\\begin{beamercolorbox}%o{%h}" "\\end{beamercolorbox}"))
183 "Environments triggered by properties in Beamer export.
184 These are the defaults - for user definitions, see
185 `org-beamer-environments-extra'.")
187 (defconst org-beamer-verbatim-elements
188 '(code example-block fixed-width inline-src-block src-block verbatim)
189 "List of element or object types producing verbatim text.
190 This is used internally to determine when a frame should have the
191 \"fragile\" option.")
195 ;;; Internal functions
197 (defun org-beamer--normalize-argument (argument type)
198 "Return ARGUMENT string with proper boundaries.
200 TYPE is a symbol among the following:
201 `action' Return ARGUMENT within angular brackets.
202 `defaction' Return ARGUMENT within both square and angular brackets.
203 `option' Return ARGUMENT within square brackets."
204 (if (not (string-match "\\S-" argument)) ""
205 (case type
206 (action (if (string-match "\\`<.*>\\'" argument) argument
207 (format "<%s>" argument)))
208 (defaction (cond
209 ((string-match "\\`\\[<.*>\\]\\'" argument) argument)
210 ((string-match "\\`<.*>\\'" argument)
211 (format "[%s]" argument))
212 ((string-match "\\`\\[\\(.*\\)\\]\\'" argument)
213 (format "[<%s>]" (match-string 1 argument)))
214 (t (format "[<%s>]" argument))))
215 (option (if (string-match "\\`\\[.*\\]\\'" argument) argument
216 (format "[%s]" argument)))
217 (otherwise argument))))
219 (defun org-beamer--element-has-overlay-p (element)
220 "Non-nil when ELEMENT has an overlay specified.
221 An element has an overlay specification when it starts with an
222 `beamer' export-snippet whose value is between angular brackets.
223 Return overlay specification, as a string, or nil."
224 (let ((first-object (car (org-element-contents element))))
225 (when (eq (org-element-type first-object) 'export-snippet)
226 (let ((value (org-element-property :value first-object)))
227 (and (string-match "\\`<.*>\\'" value) value)))))
231 ;;; Define Back-End
233 (org-export-define-derived-backend 'beamer 'latex
234 :export-block "BEAMER"
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, or a fallback value built from headline's number. This
335 function assumes HEADLINE will be treated as a frame."
336 (let ((opt (org-element-property :BEAMER_OPT headline)))
337 (if (and (stringp opt)
338 (string-match "\\(?:^\\|,\\)label=\\(.*?\\)\\(?:$\\|,\\)" opt))
339 (match-string 1 opt)
340 (format "{sec:%s}" (org-export-get-reference headline info)))))
342 (defun org-beamer--frame-level (headline info)
343 "Return frame level in subtree containing HEADLINE.
344 INFO is a plist used as a communication channel."
346 ;; 1. Look for "frame" environment in parents, starting from the
347 ;; farthest.
348 (catch 'exit
349 (dolist (parent (nreverse (org-element-lineage headline)))
350 (let ((env (org-element-property :BEAMER_ENV parent)))
351 (when (and env (member-ignore-case env '("frame" "fullframe")))
352 (throw 'exit (org-export-get-relative-level parent info))))))
353 ;; 2. Look for "frame" environment in HEADLINE.
354 (let ((env (org-element-property :BEAMER_ENV headline)))
355 (and env (member-ignore-case env '("frame" "fullframe"))
356 (org-export-get-relative-level headline info)))
357 ;; 3. Look for "frame" environment in sub-tree.
358 (org-element-map headline 'headline
359 (lambda (hl)
360 (let ((env (org-element-property :BEAMER_ENV hl)))
361 (when (and env (member-ignore-case env '("frame" "fullframe")))
362 (org-export-get-relative-level hl info))))
363 info 'first-match)
364 ;; 4. No "frame" environment in tree: use default value.
365 (plist-get info :headline-levels)))
367 (defun org-beamer--format-section (headline contents info)
368 "Format HEADLINE as a sectioning part.
369 CONTENTS holds the contents of the headline. INFO is a plist
370 used as a communication channel."
371 (let ((latex-headline
372 (org-export-with-backend
373 ;; We create a temporary export back-end which behaves the
374 ;; same as current one, but adds "\protect" in front of the
375 ;; output of some objects.
376 (org-export-create-backend
377 :parent 'latex
378 :transcoders
379 (let ((protected-output
380 (function
381 (lambda (object contents info)
382 (let ((code (org-export-with-backend
383 'beamer object contents info)))
384 (if (org-string-nw-p code) (concat "\\protect" code)
385 code))))))
386 (mapcar #'(lambda (type) (cons type protected-output))
387 '(bold footnote-reference italic strike-through timestamp
388 underline))))
389 headline
390 contents
391 info))
392 (mode-specs (org-element-property :BEAMER_ACT headline)))
393 (if (and mode-specs
394 (string-match "\\`\\\\\\(.*?\\)\\(?:\\*\\|\\[.*\\]\\)?{"
395 latex-headline))
396 ;; Insert overlay specifications.
397 (replace-match (concat (match-string 1 latex-headline)
398 (format "<%s>" mode-specs))
399 nil nil latex-headline 1)
400 latex-headline)))
402 (defun org-beamer--format-frame (headline contents info)
403 "Format HEADLINE as a frame.
404 CONTENTS holds the contents of the headline. INFO is a plist
405 used as a communication channel."
406 (let ((fragilep
407 ;; FRAGILEP is non-nil when HEADLINE contains an element
408 ;; among `org-beamer-verbatim-elements'.
409 (org-element-map headline org-beamer-verbatim-elements 'identity
410 info 'first-match)))
411 (concat "\\begin{frame}"
412 ;; Overlay specification, if any. When surrounded by
413 ;; square brackets, consider it as a default
414 ;; specification.
415 (let ((action (org-element-property :BEAMER_ACT headline)))
416 (cond
417 ((not action) "")
418 ((string-match "\\`\\[.*\\]\\'" action )
419 (org-beamer--normalize-argument action 'defaction))
420 (t (org-beamer--normalize-argument action 'action))))
421 ;; Options, if any.
422 (let* ((beamer-opt (org-element-property :BEAMER_OPT headline))
423 (options
424 ;; Collect options from default value and headline's
425 ;; properties. Also add a label for links.
426 (append
427 (org-split-string
428 (plist-get info :beamer-frame-default-options) ",")
429 (and beamer-opt
430 (org-split-string
431 ;; Remove square brackets if user provided
432 ;; them.
433 (and (string-match "^\\[?\\(.*\\)\\]?$" beamer-opt)
434 (match-string 1 beamer-opt))
435 ","))
436 ;; Provide an automatic label for the frame
437 ;; unless the user specified one. Also refrain
438 ;; from labeling `allowframebreaks' frames; this
439 ;; is not allowed by beamer.
440 (unless (and beamer-opt
441 (or (string-match "\\(^\\|,\\)label=" beamer-opt)
442 (string-match "allowframebreaks" beamer-opt)))
443 (list
444 (format "label=%s"
445 (org-beamer--get-label headline info)))))))
446 ;; Change options list into a string.
447 (org-beamer--normalize-argument
448 (mapconcat
449 'identity
450 (if (or (not fragilep) (member "fragile" options)) options
451 (cons "fragile" options))
452 ",")
453 'option))
454 ;; Title.
455 (let ((env (org-element-property :BEAMER_ENV headline)))
456 (format "{%s}"
457 (if (and env (equal (downcase env) "fullframe")) ""
458 (org-export-data
459 (org-element-property :title headline) info))))
460 "\n"
461 ;; The following workaround is required in fragile frames
462 ;; as Beamer will append "\par" to the beginning of the
463 ;; contents. So we need to make sure the command is
464 ;; separated from the contents by at least one space. If
465 ;; it isn't, it will create "\parfirst-word" command and
466 ;; remove the first word from the contents in the PDF
467 ;; output.
468 (if (not fragilep) contents
469 (replace-regexp-in-string "\\`\n*" "\\& " (or contents "")))
470 "\\end{frame}")))
472 (defun org-beamer--format-block (headline contents info)
473 "Format HEADLINE as a block.
474 CONTENTS holds the contents of the headline. INFO is a plist
475 used as a communication channel."
476 (let* ((column-width (org-element-property :BEAMER_COL headline))
477 ;; ENVIRONMENT defaults to "block" if none is specified and
478 ;; there is no column specification. If there is a column
479 ;; specified but still no explicit environment, ENVIRONMENT
480 ;; is "column".
481 (environment (let ((env (org-element-property :BEAMER_ENV headline)))
482 (cond
483 ;; "block" is the fallback environment.
484 ((and (not env) (not column-width)) "block")
485 ;; "column" only.
486 ((not env) "column")
487 ;; Use specified environment.
488 (t env))))
489 (raw-title (org-element-property :raw-value headline))
490 (env-format
491 (cond ((member environment '("column" "columns")) nil)
492 ((assoc environment
493 (append (plist-get info :beamer-environments-extra)
494 org-beamer-environments-default)))
495 (t (user-error "Wrong block type at a headline named \"%s\""
496 raw-title))))
497 (title (org-export-data (org-element-property :title headline) info))
498 (raw-options (org-element-property :BEAMER_OPT headline))
499 (options (if raw-options
500 (org-beamer--normalize-argument raw-options 'option)
501 ""))
502 ;; Start a "columns" environment when explicitly requested or
503 ;; when there is no previous headline or the previous
504 ;; headline do not have a BEAMER_column property.
505 (parent-env (org-element-property
506 :BEAMER_ENV (org-export-get-parent-headline headline)))
507 (start-columns-p
508 (or (equal environment "columns")
509 (and column-width
510 (not (and parent-env
511 (equal (downcase parent-env) "columns")))
512 (or (org-export-first-sibling-p headline info)
513 (not (org-element-property
514 :BEAMER_COL
515 (org-export-get-previous-element
516 headline info)))))))
517 ;; End the "columns" environment when explicitly requested or
518 ;; when there is no next headline or the next headline do not
519 ;; have a BEAMER_column property.
520 (end-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-last-sibling-p headline info)
526 (not (org-element-property
527 :BEAMER_COL
528 (org-export-get-next-element headline info))))))))
529 (concat
530 (when start-columns-p
531 ;; Column can accept options only when the environment is
532 ;; explicitly defined.
533 (if (not (equal environment "columns")) "\\begin{columns}\n"
534 (format "\\begin{columns}%s\n" options)))
535 (when column-width
536 (format "\\begin{column}%s{%s}\n"
537 ;; One can specify placement for column only when
538 ;; HEADLINE stands for a column on its own.
539 (if (equal environment "column") options "")
540 (format "%s\\columnwidth" column-width)))
541 ;; Block's opening string.
542 (when (nth 2 env-format)
543 (concat
544 (org-fill-template
545 (nth 2 env-format)
546 (nconc
547 ;; If BEAMER_act property has its value enclosed in square
548 ;; brackets, it is a default overlay specification and
549 ;; overlay specification is empty. Otherwise, it is an
550 ;; overlay specification and the default one is nil.
551 (let ((action (org-element-property :BEAMER_ACT headline)))
552 (cond
553 ((not action) (list (cons "a" "") (cons "A" "") (cons "R" "")))
554 ((string-match "\\`\\[.*\\]\\'" action)
555 (list
556 (cons "A" (org-beamer--normalize-argument action 'defaction))
557 (cons "a" "")
558 (cons "R" action)))
560 (list (cons "a" (org-beamer--normalize-argument action 'action))
561 (cons "A" "")
562 (cons "R" action)))))
563 (list (cons "o" options)
564 (cons "O" (or raw-options ""))
565 (cons "h" title)
566 (cons "r" raw-title)
567 (cons "H" (if (equal raw-title "") ""
568 (format "{%s}" raw-title)))
569 (cons "U" (if (equal raw-title "") ""
570 (format "[%s]" raw-title))))))
571 "\n"))
572 contents
573 ;; Block's closing string, if any.
574 (and (nth 3 env-format) (concat (nth 3 env-format) "\n"))
575 (when column-width "\\end{column}\n")
576 (when end-columns-p "\\end{columns}"))))
578 (defun org-beamer-headline (headline contents info)
579 "Transcode HEADLINE element into Beamer code.
580 CONTENTS is the contents of the headline. INFO is a plist used
581 as a communication channel."
582 (unless (org-element-property :footnote-section-p headline)
583 (let ((level (org-export-get-relative-level headline info))
584 (frame-level (org-beamer--frame-level headline info))
585 (environment (let ((env (org-element-property :BEAMER_ENV headline)))
586 (or (org-string-nw-p env) "block"))))
587 (cond
588 ;; Case 1: Resume frame specified by "BEAMER_ref" property.
589 ((equal environment "againframe")
590 (let ((ref (org-element-property :BEAMER_REF headline)))
591 ;; Reference to frame being resumed is mandatory. Ignore
592 ;; the whole headline if it isn't provided.
593 (when (org-string-nw-p ref)
594 (concat "\\againframe"
595 ;; Overlay specification.
596 (let ((overlay (org-element-property :BEAMER_ACT headline)))
597 (when overlay
598 (org-beamer--normalize-argument
599 overlay
600 (if (string-match "^\\[.*\\]$" overlay) 'defaction
601 'action))))
602 ;; Options.
603 (let ((options (org-element-property :BEAMER_OPT headline)))
604 (when options
605 (org-beamer--normalize-argument options 'option)))
606 ;; Resolve reference provided by "BEAMER_ref"
607 ;; property. This is done by building a minimal fake
608 ;; link and calling the appropriate resolve function,
609 ;; depending on the reference syntax.
610 (let* ((type
611 (progn
612 (string-match "^\\(id:\\|#\\|\\*\\)?\\(.*\\)" ref)
613 (cond
614 ((or (not (match-string 1 ref))
615 (equal (match-string 1 ref) "*")) 'fuzzy)
616 ((equal (match-string 1 ref) "id:") 'id)
617 (t 'custom-id))))
618 (link (list 'link (list :path (match-string 2 ref))))
619 (target (if (eq type 'fuzzy)
620 (org-export-resolve-fuzzy-link link info)
621 (org-export-resolve-id-link link info))))
622 ;; Now use user-defined label provided in TARGET
623 ;; headline, or fallback to standard one.
624 (format "{%s}" (org-beamer--get-label target info)))))))
625 ;; Case 2: Creation of an appendix is requested.
626 ((equal environment "appendix")
627 (concat "\\appendix"
628 (org-element-property :BEAMER_ACT headline)
629 "\n"
630 (make-string (org-element-property :pre-blank headline) ?\n)
631 contents))
632 ;; Case 3: Ignore heading.
633 ((equal environment "ignoreheading")
634 (concat (make-string (org-element-property :pre-blank headline) ?\n)
635 contents))
636 ;; Case 4: HEADLINE is a note.
637 ((member environment '("note" "noteNH"))
638 (format "\\note{%s}"
639 (concat (and (equal environment "note")
640 (concat
641 (org-export-data
642 (org-element-property :title headline) info)
643 "\n"))
644 (org-trim contents))))
645 ;; Case 5: HEADLINE is a frame.
646 ((= level frame-level)
647 (org-beamer--format-frame headline contents info))
648 ;; Case 6: Regular section, extracted from
649 ;; `org-latex-classes'.
650 ((< level frame-level)
651 (org-beamer--format-section headline contents info))
652 ;; Case 7: Otherwise, HEADLINE is a block.
653 (t (org-beamer--format-block headline contents info))))))
656 ;;;; Item
658 (defun org-beamer-item (item contents info)
659 "Transcode an ITEM element into Beamer code.
660 CONTENTS holds the contents of the item. INFO is a plist holding
661 contextual information."
662 (org-export-with-backend
663 ;; Delegate item export to `latex'. However, we use `beamer'
664 ;; transcoders for objects in the description tag.
665 (org-export-create-backend
666 :parent 'beamer
667 :transcoders
668 (list
669 (cons
670 'item
671 (lambda (item c i)
672 (let ((action
673 (let ((first (car (org-element-contents item))))
674 (and (eq (org-element-type first) 'paragraph)
675 (org-beamer--element-has-overlay-p first))))
676 (output (org-latex-item item contents info)))
677 (if (not (and action (string-match "\\\\item" output))) output
678 ;; If the item starts with a paragraph and that paragraph
679 ;; starts with an export snippet specifying an overlay,
680 ;; append it to the \item command.
681 (replace-match (concat "\\\\item" action) nil nil output)))))))
682 item contents info))
685 ;;;; Keyword
687 (defun org-beamer-keyword (keyword contents info)
688 "Transcode a KEYWORD element into Beamer code.
689 CONTENTS is nil. INFO is a plist used as a communication
690 channel."
691 (let ((key (org-element-property :key keyword))
692 (value (org-element-property :value keyword)))
693 ;; Handle specifically BEAMER and TOC (headlines only) keywords.
694 ;; Otherwise, fallback to `latex' back-end.
695 (cond
696 ((equal key "BEAMER") value)
697 ((and (equal key "TOC") (string-match "\\<headlines\\>" value))
698 (let ((depth (or (and (string-match "[0-9]+" value)
699 (string-to-number (match-string 0 value)))
700 (plist-get info :with-toc)))
701 (options (and (string-match "\\[.*?\\]" value)
702 (match-string 0 value))))
703 (concat
704 (when (wholenump depth) (format "\\setcounter{tocdepth}{%s}\n" depth))
705 "\\tableofcontents" options)))
706 (t (org-export-with-backend 'latex keyword contents info)))))
709 ;;;; Link
711 (defun org-beamer-link (link contents info)
712 "Transcode a LINK object into Beamer code.
713 CONTENTS is the description part of the link. INFO is a plist
714 used as a communication channel."
715 (let ((type (org-element-property :type link))
716 (path (org-element-property :path link)))
717 (cond
718 ;; Link type is handled by a special function.
719 ((org-export-custom-protocol-maybe link contents 'beamer))
720 ;; Use \hyperlink command for all internal links.
721 ((equal type "radio")
722 (let ((destination (org-export-resolve-radio-link link info)))
723 (if (not destination) contents
724 (format "\\hyperlink%s{%s}{%s}"
725 (or (org-beamer--element-has-overlay-p link) "")
726 (org-export-get-reference destination info)
727 contents))))
728 ((and (member type '("custom-id" "fuzzy" "id"))
729 (let ((destination (if (string= type "fuzzy")
730 (org-export-resolve-fuzzy-link link info)
731 (org-export-resolve-id-link link info))))
732 (case (org-element-type destination)
733 (headline
734 (let ((label
735 (format "sec-%s"
736 (mapconcat
737 'number-to-string
738 (org-export-get-headline-number
739 destination info)
740 "-"))))
741 (if (and (plist-get info :section-numbers) (not contents))
742 (format "\\ref{%s}" label)
743 (format "\\hyperlink%s{%s}{%s}"
744 (or (org-beamer--element-has-overlay-p link) "")
745 label
746 contents))))
747 (target
748 (let ((ref (org-export-get-reference destination info)))
749 (if (not contents) (format "\\ref{%s}" ref)
750 (format "\\hyperlink%s{%s}{%s}"
751 (or (org-beamer--element-has-overlay-p link) "")
753 contents))))))))
754 ;; Otherwise, use `latex' back-end.
755 (t (org-export-with-backend 'latex link contents info)))))
758 ;;;; Plain List
760 ;; Plain lists support `:environment', `:overlay' and `:options'
761 ;; attributes.
763 (defun org-beamer-plain-list (plain-list contents info)
764 "Transcode a PLAIN-LIST element into Beamer code.
765 CONTENTS is the contents of the list. INFO is a plist holding
766 contextual information."
767 (let* ((type (org-element-property :type plain-list))
768 (attributes (org-combine-plists
769 (org-export-read-attribute :attr_latex plain-list)
770 (org-export-read-attribute :attr_beamer plain-list)))
771 (latex-type (let ((env (plist-get attributes :environment)))
772 (cond (env)
773 ((eq type 'ordered) "enumerate")
774 ((eq type 'descriptive) "description")
775 (t "itemize")))))
776 (org-latex--wrap-label
777 plain-list
778 (format "\\begin{%s}%s%s\n%s\\end{%s}"
779 latex-type
780 ;; Default overlay specification, if any.
781 (org-beamer--normalize-argument
782 (or (plist-get attributes :overlay) "")
783 'defaction)
784 ;; Second optional argument depends on the list type.
785 (org-beamer--normalize-argument
786 (or (plist-get attributes :options) "")
787 'option)
788 ;; Eventually insert contents and close environment.
789 contents
790 latex-type)
791 info)))
794 ;;;; Radio Target
796 (defun org-beamer-radio-target (radio-target text info)
797 "Transcode a RADIO-TARGET object into Beamer code.
798 TEXT is the text of the target. INFO is a plist holding
799 contextual information."
800 (format "\\hypertarget%s{%s}{%s}"
801 (or (org-beamer--element-has-overlay-p radio-target) "")
802 (org-export-get-reference radio-target info)
803 text))
806 ;;;; Target
808 (defun org-beamer-target (target contents info)
809 "Transcode a TARGET object into Beamer code.
810 CONTENTS is nil. INFO is a plist holding contextual
811 information."
812 (format "\\label{%s}" (org-export-get-reference target info)))
815 ;;;; Template
817 ;; Template used is similar to the one used in `latex' back-end,
818 ;; excepted for the table of contents and Beamer themes.
820 (defun org-beamer-template (contents info)
821 "Return complete document string after Beamer conversion.
822 CONTENTS is the transcoded contents string. INFO is a plist
823 holding export options."
824 (let ((title (org-export-data (plist-get info :title) info))
825 (subtitle (org-export-data (plist-get info :subtitle) info)))
826 (concat
827 ;; 1. Time-stamp.
828 (and (plist-get info :time-stamp-file)
829 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
830 ;; 2. Document class and packages.
831 (let* ((class (plist-get info :latex-class))
832 (class-options (plist-get info :latex-class-options))
833 (header (nth 1 (assoc class org-latex-classes)))
834 (document-class-string
835 (and (stringp header)
836 (if (not class-options) header
837 (replace-regexp-in-string
838 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
839 class-options header t nil 1)))))
840 (if (not document-class-string)
841 (user-error "Unknown LaTeX class `%s'" class)
842 (org-latex-guess-babel-language
843 (org-latex-guess-inputenc
844 (org-element-normalize-string
845 (org-splice-latex-header
846 document-class-string
847 org-latex-default-packages-alist
848 org-latex-packages-alist nil
849 (concat (org-element-normalize-string
850 (plist-get info :latex-header))
851 (org-element-normalize-string
852 (plist-get info :latex-header-extra))))))
853 info)))
854 ;; 3. Insert themes.
855 (let ((format-theme
856 (function
857 (lambda (prop command)
858 (let ((theme (plist-get info prop)))
859 (when theme
860 (concat command
861 (if (not (string-match "\\[.*\\]" theme))
862 (format "{%s}\n" theme)
863 (format "%s{%s}\n"
864 (match-string 0 theme)
865 (org-trim
866 (replace-match "" nil nil theme)))))))))))
867 (mapconcat (lambda (args) (apply format-theme args))
868 '((:beamer-theme "\\usetheme")
869 (:beamer-color-theme "\\usecolortheme")
870 (:beamer-font-theme "\\usefonttheme")
871 (:beamer-inner-theme "\\useinnertheme")
872 (:beamer-outer-theme "\\useoutertheme"))
873 ""))
874 ;; 4. Possibly limit depth for headline numbering.
875 (let ((sec-num (plist-get info :section-numbers)))
876 (when (integerp sec-num)
877 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
878 ;; 5. Author.
879 (let ((author (and (plist-get info :with-author)
880 (let ((auth (plist-get info :author)))
881 (and auth (org-export-data auth info)))))
882 (email (and (plist-get info :with-email)
883 (org-export-data (plist-get info :email) info))))
884 (cond ((and author email (not (string= "" email)))
885 (format "\\author{%s\\thanks{%s}}\n" author email))
886 ((or author email) (format "\\author{%s}\n" (or author email)))))
887 ;; 6. Date.
888 (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
889 (format "\\date{%s}\n" (org-export-data date info)))
890 ;; 7. Title
891 (format "\\title{%s}\n" title)
892 (when (org-string-nw-p subtitle)
893 (concat (format (plist-get info :beamer-subtitle-format) subtitle) "\n"))
894 ;; 8. Beamer-header
895 (let ((beamer-header (plist-get info :beamer-header)))
896 (when beamer-header
897 (format "%s\n" (plist-get info :beamer-header))))
898 ;; 9. Hyperref options.
899 (let ((template (plist-get info :latex-hyperref-template)))
900 (and (stringp template)
901 (format-spec template (org-latex--format-spec info))))
902 ;; 10. Document start.
903 "\\begin{document}\n\n"
904 ;; 11. Title command.
905 (org-element-normalize-string
906 (cond ((not (plist-get info :with-title)) nil)
907 ((string= "" title) nil)
908 ((not (stringp org-latex-title-command)) nil)
909 ((string-match "\\(?:[^%]\\|^\\)%s"
910 org-latex-title-command)
911 (format org-latex-title-command title))
912 (t org-latex-title-command)))
913 ;; 12. Table of contents.
914 (let ((depth (plist-get info :with-toc)))
915 (when depth
916 (concat
917 (format "\\begin{frame}%s{%s}\n"
918 (org-beamer--normalize-argument
919 (plist-get info :beamer-outline-frame-options) 'option)
920 (plist-get info :beamer-outline-frame-title))
921 (when (wholenump depth)
922 (format "\\setcounter{tocdepth}{%d}\n" depth))
923 "\\tableofcontents\n"
924 "\\end{frame}\n\n")))
925 ;; 13. Document's body.
926 contents
927 ;; 14. Creator.
928 (if (plist-get info :with-creator)
929 (concat (plist-get info :creator) "\n")
931 ;; 15. Document end.
932 "\\end{document}")))
936 ;;; Minor Mode
939 (defvar org-beamer-mode-map (make-sparse-keymap)
940 "The keymap for `org-beamer-mode'.")
941 (define-key org-beamer-mode-map "\C-c\C-b" 'org-beamer-select-environment)
943 ;;;###autoload
944 (define-minor-mode org-beamer-mode
945 "Support for editing Beamer oriented Org mode files."
946 nil " Bm" 'org-beamer-mode-map)
948 (when (fboundp 'font-lock-add-keywords)
949 (font-lock-add-keywords
950 'org-mode
951 '((":\\(B_[a-z]+\\|BMCOL\\):" 1 'org-beamer-tag prepend))
952 'prepend))
954 (defface org-beamer-tag '((t (:box (:line-width 1 :color grey40))))
955 "The special face for beamer tags."
956 :group 'org-export-beamer)
958 (defun org-beamer-property-changed (property value)
959 "Track the BEAMER_env property with tags.
960 PROPERTY is the name of the modified property. VALUE is its new
961 value."
962 (cond
963 ((equal property "BEAMER_env")
964 (save-excursion
965 (org-back-to-heading t)
966 ;; Filter out Beamer-related tags and install environment tag.
967 (let ((tags (org-remove-if (lambda (x) (string-match "^B_" x))
968 (org-get-tags)))
969 (env-tag (and (org-string-nw-p value) (concat "B_" value))))
970 (org-set-tags-to (if env-tag (cons env-tag tags) tags))
971 (when env-tag (org-toggle-tag env-tag 'on)))))
972 ((equal property "BEAMER_col")
973 (org-toggle-tag "BMCOL" (if (org-string-nw-p value) 'on 'off)))))
975 (add-hook 'org-property-changed-functions 'org-beamer-property-changed)
977 (defun org-beamer-allowed-property-values (property)
978 "Supply allowed values for PROPERTY."
979 (cond
980 ((and (equal property "BEAMER_env")
981 (not (org-entry-get nil (concat property "_ALL") 'inherit)))
982 ;; If no allowed values for BEAMER_env have been defined,
983 ;; supply all defined environments
984 (mapcar 'car (append org-beamer-environments-special
985 org-beamer-environments-extra
986 org-beamer-environments-default)))
987 ((and (equal property "BEAMER_col")
988 (not (org-entry-get nil (concat property "_ALL") 'inherit)))
989 ;; If no allowed values for BEAMER_col have been defined,
990 ;; supply some
991 (org-split-string org-beamer-column-widths " "))))
993 (add-hook 'org-property-allowed-value-functions
994 'org-beamer-allowed-property-values)
998 ;;; Commands
1000 ;;;###autoload
1001 (defun org-beamer-export-as-latex
1002 (&optional async subtreep visible-only body-only ext-plist)
1003 "Export current buffer as a Beamer buffer.
1005 If narrowing is active in the current buffer, only export its
1006 narrowed part.
1008 If a region is active, export that region.
1010 A non-nil optional argument ASYNC means the process should happen
1011 asynchronously. The resulting buffer should be accessible
1012 through the `org-export-stack' interface.
1014 When optional argument SUBTREEP is non-nil, export the sub-tree
1015 at point, extracting information from the headline properties
1016 first.
1018 When optional argument VISIBLE-ONLY is non-nil, don't export
1019 contents of hidden elements.
1021 When optional argument BODY-ONLY is non-nil, only write code
1022 between \"\\begin{document}\" and \"\\end{document}\".
1024 EXT-PLIST, when provided, is a property list with external
1025 parameters overriding Org default settings, but still inferior to
1026 file-local settings.
1028 Export is done in a buffer named \"*Org BEAMER Export*\", which
1029 will be displayed when `org-export-show-temporary-export-buffer'
1030 is non-nil."
1031 (interactive)
1032 (org-export-to-buffer 'beamer "*Org BEAMER Export*"
1033 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
1035 ;;;###autoload
1036 (defun org-beamer-export-to-latex
1037 (&optional async subtreep visible-only body-only ext-plist)
1038 "Export current buffer as a Beamer presentation (tex).
1040 If narrowing is active in the current buffer, only export its
1041 narrowed part.
1043 If a region is active, export that region.
1045 A non-nil optional argument ASYNC means the process should happen
1046 asynchronously. The resulting file should be accessible through
1047 the `org-export-stack' interface.
1049 When optional argument SUBTREEP is non-nil, export the sub-tree
1050 at point, extracting information from the headline properties
1051 first.
1053 When optional argument VISIBLE-ONLY is non-nil, don't export
1054 contents of hidden elements.
1056 When optional argument BODY-ONLY is non-nil, only write code
1057 between \"\\begin{document}\" and \"\\end{document}\".
1059 EXT-PLIST, when provided, is a property list with external
1060 parameters overriding Org default settings, but still inferior to
1061 file-local settings.
1063 Return output file's name."
1064 (interactive)
1065 (let ((file (org-export-output-file-name ".tex" subtreep)))
1066 (org-export-to-file 'beamer file
1067 async subtreep visible-only body-only ext-plist)))
1069 ;;;###autoload
1070 (defun org-beamer-export-to-pdf
1071 (&optional async subtreep visible-only body-only ext-plist)
1072 "Export current buffer as a Beamer presentation (PDF).
1074 If narrowing is active in the current buffer, only export its
1075 narrowed part.
1077 If a region is active, export that region.
1079 A non-nil optional argument ASYNC means the process should happen
1080 asynchronously. The resulting file should be accessible through
1081 the `org-export-stack' interface.
1083 When optional argument SUBTREEP is non-nil, export the sub-tree
1084 at point, extracting information from the headline properties
1085 first.
1087 When optional argument VISIBLE-ONLY is non-nil, don't export
1088 contents of hidden elements.
1090 When optional argument BODY-ONLY is non-nil, only write code
1091 between \"\\begin{document}\" and \"\\end{document}\".
1093 EXT-PLIST, when provided, is a property list with external
1094 parameters overriding Org default settings, but still inferior to
1095 file-local settings.
1097 Return PDF file's name."
1098 (interactive)
1099 (let ((file (org-export-output-file-name ".tex" subtreep)))
1100 (org-export-to-file 'beamer file
1101 async subtreep visible-only body-only ext-plist
1102 (lambda (file) (org-latex-compile file)))))
1104 ;;;###autoload
1105 (defun org-beamer-select-environment ()
1106 "Select the environment to be used by beamer for this entry.
1107 While this uses (for convenience) a tag selection interface, the
1108 result of this command will be that the BEAMER_env *property* of
1109 the entry is set.
1111 In addition to this, the command will also set a tag as a visual
1112 aid, but the tag does not have any semantic meaning."
1113 (interactive)
1114 ;; Make sure `org-beamer-environments-special' has a higher
1115 ;; priority than `org-beamer-environments-extra'.
1116 (let* ((envs (append org-beamer-environments-special
1117 org-beamer-environments-extra
1118 org-beamer-environments-default))
1119 (org-tag-alist
1120 (append '((:startgroup))
1121 (mapcar (lambda (e) (cons (concat "B_" (car e))
1122 (string-to-char (nth 1 e))))
1123 envs)
1124 '((:endgroup))
1125 '(("BMCOL" . ?|))))
1126 (org-tag-persistent-alist nil)
1127 (org-use-fast-tag-selection t)
1128 (org-fast-tag-selection-single-key t))
1129 (org-set-tags)
1130 (let ((tags (or (ignore-errors (org-get-tags-string)) "")))
1131 (cond
1132 ;; For a column, automatically ask for its width.
1133 ((eq org-last-tag-selection-key ?|)
1134 (if (string-match ":BMCOL:" tags)
1135 (org-set-property "BEAMER_col" (read-string "Column width: "))
1136 (org-delete-property "BEAMER_col")))
1137 ;; For an "againframe" section, automatically ask for reference
1138 ;; to resumed frame and overlay specifications.
1139 ((eq org-last-tag-selection-key ?A)
1140 (if (equal (org-entry-get nil "BEAMER_env") "againframe")
1141 (progn (org-entry-delete nil "BEAMER_env")
1142 (org-entry-delete nil "BEAMER_ref")
1143 (org-entry-delete nil "BEAMER_act"))
1144 (org-entry-put nil "BEAMER_env" "againframe")
1145 (org-set-property
1146 "BEAMER_ref"
1147 (read-string "Frame reference (*Title, #custom-id, id:...): "))
1148 (org-set-property "BEAMER_act"
1149 (read-string "Overlay specification: "))))
1150 ((string-match (concat ":B_\\(" (mapconcat 'car envs "\\|") "\\):") tags)
1151 (org-entry-put nil "BEAMER_env" (match-string 1 tags)))
1152 (t (org-entry-delete nil "BEAMER_env"))))))
1154 ;;;###autoload
1155 (defun org-beamer-publish-to-latex (plist filename pub-dir)
1156 "Publish an Org file to a Beamer presentation (LaTeX).
1158 FILENAME is the filename of the Org file to be published. PLIST
1159 is the property list for the given project. PUB-DIR is the
1160 publishing directory.
1162 Return output file name."
1163 (org-publish-org-to 'beamer filename ".tex" plist pub-dir))
1165 ;;;###autoload
1166 (defun org-beamer-publish-to-pdf (plist filename pub-dir)
1167 "Publish an Org file to a Beamer presentation (PDF, via LaTeX).
1169 FILENAME is the filename of the Org file to be published. PLIST
1170 is the property list for the given project. PUB-DIR is the
1171 publishing directory.
1173 Return output file name."
1174 ;; Unlike to `org-beamer-publish-to-latex', PDF file is generated in
1175 ;; working directory and then moved to publishing directory.
1176 (org-publish-attachment
1177 plist
1178 (org-latex-compile
1179 (org-publish-org-to
1180 'beamer filename ".tex" plist (file-name-directory filename)))
1181 pub-dir))
1184 (provide 'ox-beamer)
1186 ;; Local variables:
1187 ;; generated-autoload-file: "org-loaddefs.el"
1188 ;; End:
1190 ;;; ox-beamer.el ends here