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