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