org-e-beamer: Allow to define frame labels
[org-mode.git] / contrib / lisp / org-e-beamer.el
blobbb6ce479225bc3002c13a86f2e0fdf303eeeeb97
1 ;;; org-e-beamer.el --- Beamer Back-End for Org Export Engine
3 ;; Copyright (C) 2007-2012 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 ;; 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.
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.
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/>.
22 ;;; Commentary:
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.
28 ;; Depending on the desired output format, three commands are provided
29 ;; for export: `org-e-beamer-export-as-latex' (temporary buffer),
30 ;; `org-e-beamer-export-to-latex' ("tex" file) and
31 ;; `org-e-beamer-export-to-pdf' ("pdf" file).
33 ;; On top of buffer keywords supported by `e-latex' back-end (see
34 ;; `org-e-latex-options-alist'), this back-end introduces the
35 ;; following keywords: "BEAMER_THEME", "BEAMER_COLOR_THEME",
36 ;; "BEAMER_FONT_THEME", "BEAMER_INNER_THEME" and "BEAMER_OUTER_THEME".
37 ;; All accept options in square brackets.
39 ;; Moreover, headlines now fall into three categories: sectioning
40 ;; elements, frames and blocks.
42 ;; - Like `e-latex' back-end sectioning elements are still set through
43 ;; `org-e-latex-classes' variable.
45 ;; - Headlines become frames when their level is equal to
46 ;; `org-e-beamer-frame-level' (or "H" value in the OPTIONS line).
47 ;; Though, if an headline in the current tree has a "BEAMER_env"
48 ;; (see below) property set to "frame", its level overrides the
49 ;; variable.
51 ;; - All frames' children become block environments. Special block
52 ;; types can be enforced by setting headline's "BEAMER_env" property
53 ;; to an appropriate value (see `org-e-beamer-environments-default'
54 ;; for supported value and `org-e-beamer-environments-extra' for
55 ;; adding more).
57 ;; - As a special case, if the "BEAMER_env" property is set to either
58 ;; "appendix", "note" or "noteNH", the headline will become,
59 ;; respectively, an appendix, a note (within frame or between frame,
60 ;; depending on its level) and a note with its title ignored.
62 ;; Also, an headline with an "ignoreheading" value will have its
63 ;; contents only inserted in the output. This special value is
64 ;; useful to have data between frames, or to properly close
65 ;; a "column" environment.
67 ;; Along with "BEAMER_env", headlines also support "BEAMER_act" and
68 ;; "BEAMER_opt" properties. The former is translated as an
69 ;; overlay/action specification (or a default overlay specification
70 ;; when enclosed within square brackets) whereas the latter specifies
71 ;; options for the current frame ("fragile" option is added
72 ;; automatically, though).
74 ;; Every plain list has support for `:overlay' attribute (through
75 ;; ATTR_BEAMER affiliated keyword). Also, ordered (resp. description)
76 ;; lists make use of `:template' (resp. `:long-text') attribute.
78 ;; Eventually, an export snippet with a value enclosed within angular
79 ;; brackets put at the beginning of an element or object whose type is
80 ;; among `bold', `item', `link', `radio-target' and `target' will
81 ;; control its overlay specifications.
83 ;; On the minor mode side, `org-e-beamer-select-environment' (bound by
84 ;; default to "C-c C-b") and `org-e-beamer-insert-options-template'
85 ;; are the two entry points.
87 ;;; Code:
89 (require 'org-e-latex)
93 ;;; User-Configurable Variables
95 (defgroup org-export-e-beamer nil
96 "Options specific for using the beamer class in LaTeX export."
97 :tag "Org Beamer"
98 :group 'org-export
99 :version "24.2")
101 (defcustom org-e-beamer-frame-level 1
102 "The level at which headlines become frames.
104 Headlines at a lower level will be translated into a sectioning
105 structure. At a higher level, they will be translated into
106 blocks.
108 If an headline with a \"BEAMER_env\" property set to \"frame\" is
109 found within a tree, its level locally overrides this number.
111 This variable has no effect on headlines with the \"BEAMER_env\"
112 property set to either \"ignoreheading\", \"appendix\", or
113 \"note\", which will respectively, be invisible, become an
114 appendix or a note.
116 This integer is relative to the minimal level of an headline
117 within the parse tree, defined as 1."
118 :group 'org-export-e-beamer
119 :type 'integer)
121 (defcustom org-e-beamer-frame-default-options ""
122 "Default options string to use for frames.
123 For example, it could be set to \"allowframebreaks\"."
124 :group 'org-export-e-beamer
125 :type '(string :tag "[options]"))
127 (defcustom org-e-beamer-column-view-format
128 "%45ITEM %10BEAMER_env(Env) %10BEAMER_act(Act) %4BEAMER_col(Col) %8BEAMER_opt(Opt)"
129 "Column view format that should be used to fill the template."
130 :group 'org-export-e-beamer
131 :type '(choice
132 (const :tag "Do not insert Beamer column view format" nil)
133 (string :tag "Beamer column view format")))
135 (defcustom org-e-beamer-theme "default"
136 "Default theme used in Beamer presentations."
137 :group 'org-export-e-beamer
138 :type '(choice
139 (const :tag "Do not insert a Beamer theme" nil)
140 (string :tag "Beamer theme")))
142 (defcustom org-e-beamer-environments-extra nil
143 "Environments triggered by tags in Beamer export.
144 Each entry has 4 elements:
146 name Name of the environment
147 key Selection key for `org-e-beamer-select-environment'
148 open The opening template for the environment, with the following escapes
149 %a the action/overlay specification
150 %A the default action/overlay specification
151 %o the options argument of the template
152 %h the headline text
153 %H if there is headline text, that text in {} braces
154 %U if there is headline text, that text in [] brackets
155 close The closing string of the environment."
156 :group 'org-export-e-beamer
157 :type '(repeat
158 (list
159 (string :tag "Environment")
160 (string :tag "Selection key")
161 (string :tag "Begin")
162 (string :tag "End"))))
164 (defcustom org-e-beamer-outline-frame-title "Outline"
165 "Default title of a frame containing an outline."
166 :group 'org-export-e-beamer
167 :type '(string :tag "Outline frame title"))
169 (defcustom org-e-beamer-outline-frame-options ""
170 "Outline frame options appended after \\begin{frame}.
171 You might want to put e.g. \"allowframebreaks=0.9\" here."
172 :group 'org-export-e-beamer
173 :type '(string :tag "Outline frame options"))
177 ;;; Internal Variables
179 (defconst org-e-beamer-column-widths
180 "0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 0.0 :ETC"
181 "The column widths that should be installed as allowed property values.")
183 (defconst org-e-beamer-environments-special
184 '(("appendix" "x")
185 ("column" "c")
186 ("frame" "f")
187 ("ignoreheading" "i")
188 ("note" "n")
189 ("noteNH" "N"))
190 "Alist of environments treated in a special way by the back-end.
191 Keys are environment names, as strings, values are bindings used
192 in `org-e-beamer-select-environment'. Environments listed here,
193 along with their binding, are hard coded and cannot be modified
194 through `org-e-beamer-environments-extra' variable.")
196 (defconst org-e-beamer-environments-default
197 '(("block" "b" "\\begin{block}%a{%h}" "\\end{block}")
198 ("alertblock" "a" "\\begin{alertblock}%a{%h}" "\\end{alertblock}")
199 ("verse" "v" "\\begin{verse}%a %% %h" "\\end{verse}")
200 ("quotation" "q" "\\begin{quotation}%a %% %h" "\\end{quotation}")
201 ("quote" "Q" "\\begin{quote}%a %% %h" "\\end{quote}")
202 ("structureenv" "s" "\\begin{structureenv}%a %% %h" "\\end{structureenv}")
203 ("theorem" "t" "\\begin{theorem}%a%U" "\\end{theorem}")
204 ("definition" "d" "\\begin{definition}%a%U" "\\end{definition}")
205 ("example" "e" "\\begin{example}%a%U" "\\end{example}")
206 ("exampleblock" "E" "\\begin{exampleblock}%a{%h}" "\\end{exampleblock}")
207 ("proof" "p" "\\begin{proof}%a%U" "\\end{proof}")
208 ("beamercolorbox" "o" "\\begin{beamercolorbox}%o{%h}" "\\end{beamercolorbox}"))
209 "Environments triggered by properties in Beamer export.
210 These are the defaults - for user definitions, see
211 `org-e-beamer-environments-extra'.")
213 (defconst org-e-beamer-verbatim-elements
214 '(code example-block fixed-width inline-src-block src-block verbatim)
215 "List of element or object types producing verbatim text.
216 This is used internally to determine when a frame should have the
217 \"fragile\" option.")
221 ;;; Internal functions
223 (defun org-e-beamer--normalize-argument (argument type)
224 "Return ARGUMENT string with proper boundaries.
226 TYPE is a symbol among the following:
227 `action' Return ARGUMENT within angular brackets.
228 `defaction' Return ARGUMENT within both square and angular brackets.
229 `option' Return ARGUMENT within square brackets."
230 (if (not (string-match "\\S-" argument)) ""
231 (case type
232 (action (if (string-match "\\`<.*>\\'" argument) argument
233 (format "<%s>" argument)))
234 (defaction (cond
235 ((string-match "\\`\\[<.*>\\]\\'" argument) argument)
236 ((string-match "\\`<.*>\\'" argument)
237 (format "[%s]" argument))
238 ((string-match "\\`\\[\\(.*\\)\\]\\'" argument)
239 (format "[<%s>]" (match-string 1 argument)))
240 (t (format "[<%s>]" argument))))
241 (option (if (string-match "\\`\\[.*\\]\\'" argument) argument
242 (format "[%s]" argument)))
243 (otherwise argument))))
245 (defun org-e-beamer--element-has-overlay-p (element)
246 "Non-nil when ELEMENT has an overlay specified.
247 An element has an overlay specification when it starts with an
248 `e-beamer' export-snippet whose value is between angular
249 brackets. Return overlay specification, as a string, or nil."
250 (let ((first-object (car (org-element-contents element))))
251 (when (eq (org-element-type first-object) 'export-snippet)
252 (let ((value (org-element-property :value first-object)))
253 (and (string-match "\\`<.*>\\'" value) value)))))
257 ;;; Define Back-End
259 (org-export-define-derived-backend e-beamer e-latex
260 :export-block "BEAMER"
261 :options-alist
262 ((:beamer-theme "BEAMER_THEME" nil org-e-beamer-theme)
263 (:beamer-color-theme "BEAMER_COLOR_THEME" nil nil t)
264 (:beamer-font-theme "BEAMER_FONT_THEME" nil nil t)
265 (:beamer-inner-theme "BEAMER_INNER_THEME" nil nil t)
266 (:beamer-outer-theme "BEAMER_OUTER_THEME" nil nil t)
267 (:headline-levels nil "H" org-e-beamer-frame-level))
268 :translate-alist ((bold . org-e-beamer-bold)
269 (export-block . org-e-beamer-export-block)
270 (export-snippet . org-e-beamer-export-snippet)
271 (headline . org-e-beamer-headline)
272 (item . org-e-beamer-item)
273 (keyword . org-e-beamer-keyword)
274 (link . org-e-beamer-link)
275 (plain-list . org-e-beamer-plain-list)
276 (radio-target . org-e-beamer-radio-target)
277 (target . org-e-beamer-target)
278 (template . org-e-beamer-template)))
282 ;;; Transcode Functions
284 ;;;; Bold
286 (defun org-e-beamer-bold (bold contents info)
287 "Transcode BLOCK object into Beamer code.
288 CONTENTS is the text being bold. INFO is a plist used as
289 a communication channel."
290 (format "\\alert%s{%s}"
291 (or (org-e-beamer--element-has-overlay-p bold) "")
292 contents))
295 ;;;; Export Block
297 (defun org-e-beamer-export-block (export-block contents info)
298 "Transcode an EXPORT-BLOCK element into Beamer code.
299 CONTENTS is nil. INFO is a plist used as a communication
300 channel."
301 (when (member (org-element-property :type export-block) '("BEAMER" "LATEX"))
302 (org-remove-indentation (org-element-property :value export-block))))
305 ;;;; Export Snippet
307 (defun org-e-beamer-export-snippet (export-snippet contents info)
308 "Transcode an EXPORT-SNIPPET object into Beamer code.
309 CONTENTS is nil. INFO is a plist used as a communication
310 channel."
311 (let ((backend (org-export-snippet-backend export-snippet))
312 (value (org-element-property :value export-snippet)))
313 ;; Only "e-latex" and "e-beamer" snippets are retained.
314 (cond ((eq backend 'e-latex) value)
315 ;; Ignore "e-beamer" snippets specifying overlays.
316 ((and (eq backend 'e-beamer)
317 (or (org-export-get-previous-element export-snippet info)
318 (not (string-match "\\`<.*>\\'" value))))
319 value))))
322 ;;;; Headline
324 ;; The main function to translate an headline is
325 ;; `org-e-beamer-headline'.
327 ;; Depending on the level at which an headline is considered as
328 ;; a frame (given by `org-e-beamer--frame-level'), the headline is
329 ;; either a section (`org-e-beamer--format-section'), a frame
330 ;; (`org-e-beamer--format-frame') or a block
331 ;; (`org-e-beamer--format-block').
333 ;; `org-e-beamer-headline' also takes care of special environments
334 ;; like "ignoreheading", "note", "noteNH" and "appendix".
336 (defun org-e-beamer--frame-level (headline info)
337 "Return frame level in subtree containing HEADLINE.
338 INFO is a plist used as a communication channel."
340 ;; 1. Look for "frame" environment in parents, starting from the
341 ;; farthest.
342 (catch 'exit
343 (mapc (lambda (parent)
344 (when (equal (org-element-property :beamer-env parent) "frame")
345 (throw 'exit (org-export-get-relative-level parent info))))
346 (reverse (org-export-get-genealogy headline)))
347 nil)
348 ;; 2. Look for "frame" environment in HEADLINE.
349 (and (equal (org-element-property :beamer-env headline) "frame")
350 (org-export-get-relative-level headline info))
351 ;; 3. Look for "frame" environment in sub-tree.
352 (org-element-map
353 headline 'headline
354 (lambda (hl)
355 (when (equal (org-element-property :beamer-env hl) "frame")
356 (org-export-get-relative-level hl info)))
357 info 'first-match)
358 ;; 4. No "frame" environment in tree: use default value.
359 (plist-get info :headline-levels)))
361 (defun org-e-beamer--format-section (headline contents info)
362 "Format HEADLINE as a sectioning part.
363 CONTENTS holds the contents of the headline. INFO is a plist
364 used as a communication channel."
365 ;; Use `e-latex' back-end output, inserting overlay specifications
366 ;; if possible.
367 (let ((latex-headline
368 (funcall (cdr (assq 'headline org-e-latex-translate-alist))
369 headline contents info))
370 (mode-specs (org-element-property :beamer-act headline)))
371 (if (and mode-specs
372 (string-match "\\`\\\\\\(.*?\\)\\(?:\\*\\|\\[.*\\]\\)?{"
373 latex-headline))
374 (replace-match (concat (match-string 1 latex-headline)
375 (format "<%s>" mode-specs))
376 nil nil latex-headline 1)
377 latex-headline)))
379 (defun org-e-beamer--format-frame (headline contents info)
380 "Format HEADLINE as a frame.
381 CONTENTS holds the contents of the headline. INFO is a plist
382 used as a communication channel."
383 (let ((fragilep
384 ;; FRAGILEP is non-nil when HEADLINE contains an element
385 ;; among `org-e-beamer-verbatim-elements'.
386 (org-element-map headline org-e-beamer-verbatim-elements 'identity
387 info 'first-match)))
388 (concat "\\begin{frame}"
389 ;; Overlay specification, if any. When surrounded by
390 ;; square brackets, consider it as a default
391 ;; specification.
392 (let ((action (org-element-property :beamer-act headline)))
393 (cond
394 ((not action) "")
395 ((string-match "\\`\\[.*\\]\\'" action )
396 (org-e-beamer--normalize-argument action 'defaction))
397 (t (org-e-beamer--normalize-argument action 'action))))
398 ;; Options, if any.
399 (let* ((beamer-opt (org-element-property :beamer-opt headline))
400 (options
401 ;; Collect options from default value and headline's
402 ;; properties. Also add a label for links.
403 (append
404 (org-split-string org-e-beamer-frame-default-options ",")
405 (and beamer-opt
406 (org-split-string
407 ;; Remove square brackets if user provided
408 ;; them.
409 (and (string-match "^\\[?\\(.*\\)\\]?$" beamer-opt)
410 (match-string 1 beamer-opt))
411 ","))
412 ;; Provide an automatic label for the frame
413 ;; unless the user specified one.
414 (unless (and beamer-opt
415 (string-match "\\(^\\|,\\)label=" beamer-opt))
416 (list
417 (format "label=sec-%s"
418 (mapconcat
419 'number-to-string
420 (org-export-get-headline-number headline info)
421 "-")))))))
422 ;; Change options list into a string.
423 (org-e-beamer--normalize-argument
424 (mapconcat
425 'identity
426 (if (or (not fragilep) (member "fragile" options)) options
427 (cons "fragile" options))
428 ",")
429 'option))
430 ;; Title.
431 (format "{%s}"
432 (org-export-data (org-element-property :title headline)
433 info))
434 "\n"
435 ;; The following workaround is required in fragile frames
436 ;; as Beamer will append "\par" to the beginning of the
437 ;; contents. So we need to make sure the command is
438 ;; separated from the contents by at least one space. If
439 ;; it isn't, it will create "\parfirst-word" command and
440 ;; remove the first word from the contents in the PDF
441 ;; output.
442 (if (not fragilep) contents
443 (replace-regexp-in-string "\\`\n*" "\\& " contents))
444 "\\end{frame}")))
446 (defun org-e-beamer--format-block (headline contents info)
447 "Format HEADLINE as a block.
448 CONTENTS holds the contents of the headline. INFO is a plist
449 used as a communication channel."
450 (let* ((column-width (org-element-property :beamer-col headline))
451 ;; Environment defaults to "block" if none is specified and
452 ;; there is no column specification. If there is a column
453 ;; specified but still no explicit environment, ENVIRONMENT
454 ;; is nil.
455 (environment (let ((env (org-element-property :beamer-env headline)))
456 (cond
457 ;; "block" is the fallback environment.
458 ((and (not env) (not column-width)) "block")
459 ;; "column" only.
460 ((not env) nil)
461 ;; Use specified environment.
462 (t (downcase env)))))
463 (env-format (when environment
464 (assoc environment
465 (append org-e-beamer-environments-special
466 org-e-beamer-environments-extra
467 org-e-beamer-environments-default))))
468 (title (org-export-data (org-element-property :title headline) info))
469 ;; Start a columns environment when there is no previous
470 ;; headline or the previous headline do not have
471 ;; a BEAMER_column property.
472 (start-columns-p
473 (and column-width
474 (or (org-export-first-sibling-p headline info)
475 (not (org-element-property
476 :beamer-col
477 (org-export-get-previous-element headline info))))))
478 ;; Ends a columns environment when there is no next headline
479 ;; or the next headline do not have a BEAMER_column property.
480 (end-columns-p
481 (and column-width
482 (or (org-export-last-sibling-p headline info)
483 (not (org-element-property
484 :beamer-col
485 (org-export-get-next-element headline info)))))))
486 (concat
487 (when start-columns-p "\\begin{columns}\n")
488 (when column-width
489 (format "\\begin{column}%s{%s}\n"
490 ;; One can specify placement for column only when
491 ;; HEADLINE stands for a column on its own.
492 (if (not environment) ""
493 (let ((options (org-element-property :beamer-opt headline)))
494 (if (not options) ""
495 (org-e-beamer--normalize-argument options 'option))))
496 (format "%s\\textwidth" column-width)))
497 ;; Block's opening string.
498 (when env-format
499 (concat
500 (org-fill-template
501 (nth 2 env-format)
502 (nconc
503 ;; If BEAMER_act property has its value enclosed in square
504 ;; brackets, it is a default overlay specification and
505 ;; overlay specification is empty. Otherwise, it is an
506 ;; overlay specification and the default one is nil.
507 (let ((action (org-element-property :beamer-act headline)))
508 (cond
509 ((not action) (list (cons "a" "") (cons "A" "")))
510 ((string-match "\\`\\[.*\\]\\'" action)
511 (list
512 (cons "A"
513 (org-e-beamer--normalize-argument action 'defaction))
514 (cons "a" "")))
516 (list
517 (cons "a"
518 (org-e-beamer--normalize-argument action 'action))
519 (cons "A" "")))))
520 (list (cons "o"
521 (let ((options
522 (org-element-property :beamer-opt headline)))
523 (if (not options) ""
524 (org-e-beamer--normalize-argument options 'option))))
525 (cons "h" title)
526 (cons "H" (if (equal title "") "" (format "{%s}" title)))
527 (cons "U" (if (equal title "") "" (format "[%s]" title))))))
528 "\n"))
529 contents
530 ;; Block's closing string.
531 (when environment (concat (nth 3 env-format) "\n"))
532 (when column-width "\\end{column}\n")
533 (when end-columns-p "\\end{columns}"))))
535 (defun org-e-beamer-headline (headline contents info)
536 "Transcode HEADLINE element into Beamer code.
537 CONTENTS is the contents of the headline. INFO is a plist used
538 as a communication channel."
539 (unless (org-element-property :footnote-section-p headline)
540 (let ((level (org-export-get-relative-level headline info))
541 (frame-level (org-e-beamer--frame-level headline info))
542 (environment (let ((env (org-element-property :beamer-env headline)))
543 (if (stringp env) (downcase env) "block"))))
544 (cond
545 ;; Creation of an appendix is requested.
546 ((equal environment "appendix")
547 (concat "\\appendix"
548 (org-element-property :beamer-act headline)
549 "\n"
550 (make-string (org-element-property :pre-blank headline) ?\n)
551 contents))
552 ((equal environment "ignoreheading")
553 (concat (make-string (org-element-property :pre-blank headline) ?\n)
554 contents))
555 ;; HEADLINE is a note.
556 ((member environment '("note" "noteNH"))
557 (format "\\note{%s}"
558 (concat (and (equal environment "note")
559 (concat
560 (org-export-data
561 (org-element-property :title headline) info)
562 "\n"))
563 (org-trim contents))))
564 ;; HEADLINE is a frame.
565 ((or (equal environment "frame") (= level frame-level))
566 (org-e-beamer--format-frame headline contents info))
567 ;; Regular section, extracted from `org-e-latex-classes'.
568 ((< level frame-level)
569 (org-e-beamer--format-section headline contents info))
570 ;; Otherwise, HEADLINE is a block.
571 (t (org-e-beamer--format-block headline contents info))))))
574 ;;;; Item
576 (defun org-e-beamer-item (item contents info)
577 "Transcode an ITEM element into Beamer code.
578 CONTENTS holds the contents of the item. INFO is a plist holding
579 contextual information."
580 (let ((action (let ((first-element (car (org-element-contents item))))
581 (and (eq (org-element-type first-element) 'paragraph)
582 (org-e-beamer--element-has-overlay-p first-element))))
583 (output (funcall (cdr (assq 'item org-e-latex-translate-alist))
584 item contents info)))
585 (if (not action) output
586 ;; If the item starts with a paragraph and that paragraph starts
587 ;; with an export snippet specifying an overlay, insert it after
588 ;; \item command.
589 (replace-regexp-in-string "\\\\item" (concat "\\\\item" action) output))))
592 ;;;; Keyword
594 (defun org-e-beamer-keyword (keyword contents info)
595 "Transcode a KEYWORD element into Beamer code.
596 CONTENTS is nil. INFO is a plist used as a communication
597 channel."
598 (let ((key (org-element-property :key keyword))
599 (value (org-element-property :value keyword)))
600 ;; Handle specifically BEAMER and TOC (headlines only) keywords.
601 ;; Otherwise, fallback to `e-latex' back-end.
602 (cond
603 ((equal key "BEAMER") value)
604 ((and (equal key "TOC") (string-match "\\<headlines\\>" value))
605 (let ((depth (or (and (string-match "[0-9]+" value)
606 (string-to-number (match-string 0 value)))
607 (plist-get info :with-toc)))
608 (options (and (string-match "\\[.*?\\]" value)
609 (match-string 0 value))))
610 (concat
611 "\\begin{frame}"
612 (when (wholenump depth) (format "\\setcounter{tocdepth}{%s}\n" depth))
613 "\\tableofcontents" options "\n"
614 "\\end{frame}")))
615 (t (funcall (cdr (assq 'keyword org-e-latex-translate-alist))
616 keyword contents info)))))
619 ;;;; Link
621 (defun org-e-beamer-link (link contents info)
622 "Transcode a LINK object into Beamer code.
623 CONTENTS is the description part of the link. INFO is a plist
624 used as a communication channel."
625 (let ((type (org-element-property :type link))
626 (path (org-element-property :path link)))
627 ;; Use \hyperlink command for all internal links.
628 (cond
629 ((equal type "radio")
630 (let ((destination (org-export-resolve-radio-link link info)))
631 (when destination
632 (format "\\hyperlink%s{%s}{%s}"
633 (or (org-e-beamer--element-has-overlay-p link) "")
634 (org-export-solidify-link-text path)
635 (org-export-data (org-element-contents destination) info)))))
636 ((and (member type '("custom-id" "fuzzy" "id"))
637 (let ((destination (if (string= type "fuzzy")
638 (org-export-resolve-fuzzy-link link info)
639 (org-export-resolve-id-link link info))))
640 (case (org-element-type destination)
641 (headline
642 (let ((label
643 (format "sec-%s"
644 (mapconcat
645 'number-to-string
646 (org-export-get-headline-number
647 destination info)
648 "-"))))
649 (if (and (plist-get info :section-numbers) (not contents))
650 (format "\\ref{%s}" label)
651 (format "\\hyperlink%s{%s}{%s}"
652 (or (org-e-beamer--element-has-overlay-p link) "")
653 label
654 contents))))
655 (target
656 (let ((path (org-export-solidify-link-text path)))
657 (if (not contents) (format "\\ref{%s}" path)
658 (format "\\hyperlink%s{%s}{%s}"
659 (or (org-e-beamer--element-has-overlay-p link) "")
660 path
661 contents))))))))
662 ;; Otherwise, use `e-latex' back-end.
663 (t (funcall (cdr (assq 'link org-e-latex-translate-alist))
664 link contents info)))))
667 ;;;; Plain List
669 ;; Plain lists support `:overlay' (for any type), `:template' (for
670 ;; ordered lists only) and `:long-text' (for description lists only)
671 ;; attributes.
673 (defun org-e-beamer-plain-list (plain-list contents info)
674 "Transcode a PLAIN-LIST element into Beamer code.
675 CONTENTS is the contents of the list. INFO is a plist holding
676 contextual information."
677 (let* ((type (org-element-property :type plain-list))
678 (attributes (org-export-read-attribute :attr_beamer plain-list))
679 (latex-type (cond ((eq type 'ordered) "enumerate")
680 ((eq type 'descriptive) "description")
681 (t "itemize"))))
682 (org-e-latex--wrap-label
683 plain-list
684 (format "\\begin{%s}%s%s\n%s\\end{%s}"
685 latex-type
686 ;; Default overlay specification, if any.
687 (let ((overlay (plist-get attributes :overlay)))
688 (if (not overlay) ""
689 (org-e-beamer--normalize-argument overlay 'defaction)))
690 ;; Second optional argument depends on the list type.
691 (case type
692 (ordered
693 (let ((template (plist-get attributes :template)))
694 (if (not template) ""
695 (org-e-beamer--normalize-argument template 'option))))
696 (descriptive
697 (let ((long-text (plist-get attributes :long-text)))
698 (if (not long-text) ""
699 (org-e-beamer--normalize-argument long-text 'option))))
700 ;; There's no second argument for un-ordered lists.
701 (otherwise ""))
702 ;; Eventually insert contents and close environment.
703 contents
704 latex-type))))
707 ;;;; Radio Target
709 (defun org-e-beamer-radio-target (radio-target text info)
710 "Transcode a RADIO-TARGET object into Beamer code.
711 TEXT is the text of the target. INFO is a plist holding
712 contextual information."
713 (format "\\hypertarget%s{%s}{%s}"
714 (or (org-e-beamer--element-has-overlay-p radio-target) "")
715 (org-export-solidify-link-text
716 (org-element-property :value radio-target))
717 text))
720 ;;;; Target
722 (defun org-e-beamer-target (target contents info)
723 "Transcode a TARGET object into Beamer code.
724 CONTENTS is nil. INFO is a plist holding contextual
725 information."
726 (format "\\hypertarget{%s}{}"
727 (org-export-solidify-link-text (org-element-property :value target))))
730 ;;;; Template
732 ;; Template used is similar to the one used in `e-latex' back-end,
733 ;; excepted for the table of contents and Beamer themes.
735 (defun org-e-beamer-template (contents info)
736 "Return complete document string after Beamer conversion.
737 CONTENTS is the transcoded contents string. INFO is a plist
738 holding export options."
739 (let ((title (org-export-data (plist-get info :title) info)))
740 (concat
741 ;; 1. Time-stamp.
742 (and (plist-get info :time-stamp-file)
743 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
744 ;; 2. Document class and packages.
745 (let ((class (plist-get info :latex-class))
746 (class-options (plist-get info :latex-class-options)))
747 (org-element-normalize-string
748 (let* ((header (nth 1 (assoc class org-e-latex-classes)))
749 (document-class-string
750 (and (stringp header)
751 (if (not class-options) header
752 (replace-regexp-in-string
753 "^[ \t]*\\\\documentclass\\(\\(\\[.*\\]\\)?\\)"
754 class-options header t nil 1)))))
755 (when document-class-string
756 (org-e-latex--guess-babel-language
757 (org-e-latex--guess-inputenc
758 (org-splice-latex-header
759 document-class-string
760 org-export-latex-default-packages-alist ; defined in org.el
761 org-export-latex-packages-alist nil ; defined in org.el
762 (plist-get info :latex-header-extra)))
763 info)))))
764 ;; 3. Insert themes.
765 (let ((format-theme
766 (function
767 (lambda (prop command)
768 (let ((theme (plist-get info prop)))
769 (when theme
770 (concat command
771 (if (not (string-match "\\[.*\\]" theme))
772 (format "{%s}\n" theme)
773 (format "%s{%s}\n"
774 (match-string 0 theme)
775 (org-trim
776 (replace-match "" nil nil theme)))))))))))
777 (mapconcat (lambda (args) (apply format-theme args))
778 '((:beamer-theme "\\usetheme")
779 (:beamer-color-theme "\\usecolortheme")
780 (:beamer-font-theme "\\usefonttheme")
781 (:beamer-inner-theme "\\useinnertheme")
782 (:beamer-outer-theme "\\useoutertheme"))
783 ""))
784 ;; 4. Possibly limit depth for headline numbering.
785 (let ((sec-num (plist-get info :section-numbers)))
786 (when (integerp sec-num)
787 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
788 ;; 5. Author.
789 (let ((author (and (plist-get info :with-author)
790 (let ((auth (plist-get info :author)))
791 (and auth (org-export-data auth info)))))
792 (email (and (plist-get info :with-email)
793 (org-export-data (plist-get info :email) info))))
794 (cond ((and author email (not (string= "" email)))
795 (format "\\author{%s\\thanks{%s}}\n" author email))
796 (author (format "\\author{%s}\n" author))
797 (t "\\author{}\n")))
798 ;; 6. Date.
799 (format "\\date{%s}\n" (org-export-data (plist-get info :date) info))
800 ;; 7. Title
801 (format "\\title{%s}\n" title)
802 ;; 8. Hyperref options.
803 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
804 (or (plist-get info :keywords) "")
805 (or (plist-get info :description) "")
806 (if (not (plist-get info :with-creator)) ""
807 (plist-get info :creator)))
808 ;; 9. Document start.
809 "\\begin{document}\n\n"
810 ;; 10. Title command.
811 (org-element-normalize-string
812 (cond ((string= "" title) nil)
813 ((not (stringp org-e-latex-title-command)) nil)
814 ((string-match "\\(?:[^%]\\|^\\)%s"
815 org-e-latex-title-command)
816 (format org-e-latex-title-command title))
817 (t org-e-latex-title-command)))
818 ;; 11. Table of contents.
819 (let ((depth (plist-get info :with-toc)))
820 (when depth
821 (concat
822 (format "\\begin{frame}%s{%s}\n"
823 (org-e-beamer--normalize-argument
824 org-e-beamer-outline-frame-options 'option)
825 org-e-beamer-outline-frame-title)
826 (when (wholenump depth)
827 (format "\\setcounter{tocdepth}{%d}\n" depth))
828 "\\tableofcontents\n"
829 "\\end{frame}\n\n")))
830 ;; 12. Document's body.
831 contents
832 ;; 13. Creator.
833 (let ((creator-info (plist-get info :with-creator)))
834 (cond
835 ((not creator-info) "")
836 ((eq creator-info 'comment)
837 (format "%% %s\n" (plist-get info :creator)))
838 (t (concat (plist-get info :creator) "\n"))))
839 ;; 14. Document end.
840 "\\end{document}")))
844 ;;; Minor Mode
847 (defvar org-e-beamer-mode-map (make-sparse-keymap)
848 "The keymap for `org-e-beamer-mode'.")
849 (define-key org-e-beamer-mode-map "\C-c\C-b" 'org-e-beamer-select-environment)
851 ;;;###autoload
852 (define-minor-mode org-e-beamer-mode
853 "Support for editing Beamer oriented Org mode files."
854 nil " Bm" 'org-e-beamer-mode-map)
856 (when (fboundp 'font-lock-add-keywords)
857 (font-lock-add-keywords
858 'org-mode
859 '((":\\(B_[a-z]+\\|BMCOL\\):" 1 'org-e-beamer-tag prepend))
860 'prepend))
862 (defface org-e-beamer-tag '((t (:box (:line-width 1 :color grey40))))
863 "The special face for beamer tags."
864 :group 'org-export-e-beamer)
866 (defun org-e-beamer-property-changed (property value)
867 "Track the BEAMER_env property with tags.
868 PROPERTY is the name of the modified property. VALUE is its new
869 value."
870 (cond
871 ((equal property "BEAMER_env")
872 (save-excursion
873 (org-back-to-heading t)
874 (let ((tags (org-get-tags)))
875 (setq tags (delq nil (mapcar (lambda (x)
876 (if (string-match "^B_" x) nil x))
877 tags)))
878 (org-set-tags-to tags))
879 (when (org-string-nw-p value) (org-toggle-tag (concat "B_" value) 'on))))
880 ((equal property "BEAMER_col")
881 (org-toggle-tag "BMCOL" (if (org-string-nw-p value) 'on 'off)))))
883 (add-hook 'org-property-changed-functions 'org-e-beamer-property-changed)
885 (defun org-e-beamer-allowed-property-values (property)
886 "Supply allowed values for PROPERTY."
887 (cond
888 ((and (equal property "BEAMER_env")
889 (not (org-entry-get nil (concat property "_ALL") 'inherit)))
890 ;; If no allowed values for BEAMER_env have been defined,
891 ;; supply all defined environments
892 (mapcar 'car (append org-e-beamer-environments-special
893 org-e-beamer-environments-extra
894 org-e-beamer-environments-default)))
895 ((and (equal property "BEAMER_col")
896 (not (org-entry-get nil (concat property "_ALL") 'inherit)))
897 ;; If no allowed values for BEAMER_col have been defined,
898 ;; supply some
899 (org-split-string org-e-beamer-column-widths " "))))
901 (add-hook 'org-property-allowed-value-functions
902 'org-e-beamer-allowed-property-values)
906 ;;; Commands
908 ;;;###autoload
909 (defun org-e-beamer-export-as-latex
910 (&optional subtreep visible-only body-only ext-plist)
911 "Export current buffer as a Beamer buffer.
913 If narrowing is active in the current buffer, only export its
914 narrowed part.
916 If a region is active, export that region.
918 When optional argument SUBTREEP is non-nil, export the sub-tree
919 at point, extracting information from the headline properties
920 first.
922 When optional argument VISIBLE-ONLY is non-nil, don't export
923 contents of hidden elements.
925 When optional argument BODY-ONLY is non-nil, only write code
926 between \"\\begin{document}\" and \"\\end{document}\".
928 EXT-PLIST, when provided, is a property list with external
929 parameters overriding Org default settings, but still inferior to
930 file-local settings.
932 Export is done in a buffer named \"*Org E-BEAMER Export*\", which
933 will be displayed when `org-export-show-temporary-export-buffer'
934 is non-nil."
935 (interactive)
936 (let ((outbuf (org-export-to-buffer
937 'e-beamer "*Org E-BEAMER Export*"
938 subtreep visible-only body-only ext-plist)))
939 (with-current-buffer outbuf (LaTeX-mode))
940 (when org-export-show-temporary-export-buffer
941 (switch-to-buffer-other-window outbuf))))
943 ;;;###autoload
944 (defun org-e-beamer-export-to-latex
945 (&optional subtreep visible-only body-only ext-plist pub-dir)
946 "Export current buffer as a Beamer presentation (tex).
948 If narrowing is active in the current buffer, only export its
949 narrowed part.
951 If a region is active, export that region.
953 When optional argument SUBTREEP is non-nil, export the sub-tree
954 at point, extracting information from the headline properties
955 first.
957 When optional argument VISIBLE-ONLY is non-nil, don't export
958 contents of hidden elements.
960 When optional argument BODY-ONLY is non-nil, only write code
961 between \"\\begin{document}\" and \"\\end{document}\".
963 EXT-PLIST, when provided, is a property list with external
964 parameters overriding Org default settings, but still inferior to
965 file-local settings.
967 When optional argument PUB-DIR is set, use it as the publishing
968 directory.
970 Return output file's name."
971 (interactive)
972 (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir)))
973 (org-export-to-file
974 'e-beamer outfile subtreep visible-only body-only ext-plist)))
976 ;;;###autoload
977 (defun org-e-beamer-export-to-pdf
978 (&optional subtreep visible-only body-only ext-plist pub-dir)
979 "Export current buffer as a Beamer presentation (PDF).
981 If narrowing is active in the current buffer, only export its
982 narrowed part.
984 If a region is active, export that region.
986 When optional argument SUBTREEP is non-nil, export the sub-tree
987 at point, extracting information from the headline properties
988 first.
990 When optional argument VISIBLE-ONLY is non-nil, don't export
991 contents of hidden elements.
993 When optional argument BODY-ONLY is non-nil, only write code
994 between \"\\begin{document}\" and \"\\end{document}\".
996 EXT-PLIST, when provided, is a property list with external
997 parameters overriding Org default settings, but still inferior to
998 file-local settings.
1000 When optional argument PUB-DIR is set, use it as the publishing
1001 directory.
1003 Return PDF file's name."
1004 (interactive)
1005 (org-e-latex-compile
1006 (org-e-beamer-export-to-latex
1007 subtreep visible-only body-only ext-plist pub-dir)))
1009 ;;;###autoload
1010 (defun org-e-beamer-select-environment ()
1011 "Select the environment to be used by beamer for this entry.
1012 While this uses (for convenience) a tag selection interface, the
1013 result of this command will be that the BEAMER_env *property* of
1014 the entry is set.
1016 In addition to this, the command will also set a tag as a visual
1017 aid, but the tag does not have any semantic meaning."
1018 (interactive)
1019 ;; Make sure `org-e-beamer-environments-special' has a higher
1020 ;; priority than `org-e-beamer-environments-extra'.
1021 (let* ((envs (append org-e-beamer-environments-special
1022 org-e-beamer-environments-extra
1023 org-e-beamer-environments-default))
1024 (org-tag-alist
1025 (append '((:startgroup))
1026 (mapcar (lambda (e) (cons (concat "B_" (car e))
1027 (string-to-char (nth 1 e))))
1028 envs)
1029 '((:endgroup))
1030 '(("BMCOL" . ?|))))
1031 (org-fast-tag-selection-single-key t))
1032 (org-set-tags)
1033 (let ((tags (or (ignore-errors (org-get-tags-string)) "")))
1034 (cond
1035 ((eq org-last-tag-selection-key ?|)
1036 (if (string-match ":BMCOL:" tags)
1037 (org-set-property "BEAMER_col" (read-string "Column width: "))
1038 (org-delete-property "BEAMER_col")))
1039 ((string-match (concat ":B_\\("
1040 (mapconcat 'car envs "\\|")
1041 "\\):")
1042 tags)
1043 (org-entry-put nil "BEAMER_env" (match-string 1 tags)))
1044 (t (org-entry-delete nil "BEAMER_env"))))))
1046 ;;;###autoload
1047 (defun org-e-beamer-insert-options-template (&optional kind)
1048 "Insert a settings template, to make sure users do this right."
1049 (interactive (progn
1050 (message "Current [s]ubtree or [g]lobal?")
1051 (if (eq (read-char-exclusive) ?g) (list 'global)
1052 (list 'subtree))))
1053 (if (eq kind 'subtree)
1054 (progn
1055 (org-back-to-heading t)
1056 (org-reveal)
1057 (org-entry-put nil "LaTeX_CLASS" "beamer")
1058 (org-entry-put nil "LaTeX_CLASS_OPTIONS" "[presentation]")
1059 (org-entry-put nil "EXPORT_FILE_NAME" "presentation.pdf")
1060 (when org-e-beamer-column-view-format
1061 (org-entry-put nil "COLUMNS" org-e-beamer-column-view-format))
1062 (org-entry-put nil "BEAMER_col_ALL" org-e-beamer-column-widths))
1063 (insert "#+LaTeX_CLASS: beamer\n")
1064 (insert "#+LaTeX_CLASS_OPTIONS: [presentation]\n")
1065 (when org-e-beamer-theme
1066 (insert "#+BEAMER_THEME: " org-e-beamer-theme "\n"))
1067 (when org-e-beamer-column-view-format
1068 (insert "#+COLUMNS: " org-e-beamer-column-view-format "\n"))
1069 (insert "#+PROPERTY: BEAMER_col_ALL " org-e-beamer-column-widths "\n")))
1072 (provide 'org-e-beamer)
1073 ;;; org-e-beamer.el ends here