Backwards compatibility for `user-errorĀ“
[org-mode/org-tableheadings.git] / contrib / lisp / org-e-beamer.el
blob0c3c430c558b3e3f5beda3503a9cc1d28cf90b90
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. If is surrounded by square
390 ;; brackets, consider it as a default specification.
391 (let ((action (org-element-property :beamer-act headline)))
392 (cond
393 ((not action) "")
394 ((string-match "\\`\\[.*\\]\\'" action )
395 (org-e-beamer--normalize-argument action 'defaction))
396 (t (org-e-beamer--normalize-argument action 'action))))
397 ;; Options, if any.
398 (let ((options
399 ;; Collect options from default value and headline's
400 ;; properties. Also add a label for links.
401 (append
402 (org-split-string org-e-beamer-frame-default-options
403 ",")
404 (let ((opt (org-element-property :beamer-opt headline)))
405 (and opt (org-split-string
406 ;; Remove square brackets if user
407 ;; provided them.
408 (and (string-match "^\\[?\\(.*\\)\\]?$" opt)
409 (match-string 1 opt))
410 ",")))
411 (list
412 (format "label=sec-%s"
413 (mapconcat
414 'number-to-string
415 (org-export-get-headline-number headline info)
416 "-"))))))
417 ;; Change options list into a string.
418 (org-e-beamer--normalize-argument
419 (mapconcat
420 'identity
421 (if (or (not fragilep) (member "fragile" options)) options
422 (cons "fragile" options))
423 ",")
424 'option))
425 ;; Title.
426 (format "{%s}"
427 (org-export-data (org-element-property :title headline)
428 info))
429 "\n"
430 ;; The following workaround is required in fragile frames
431 ;; as Beamer will append "\par" to the beginning of the
432 ;; contents. So we need to make sure the command is
433 ;; separated from the contents by at least one space. If
434 ;; it isn't, it will create "\parfirst-word" command and
435 ;; remove the first word from the contents in the PDF
436 ;; output.
437 (if (not fragilep) contents
438 (replace-regexp-in-string "\\`\n*" "\\& " contents))
439 "\\end{frame}")))
441 (defun org-e-beamer--format-block (headline contents info)
442 "Format HEADLINE as a block.
443 CONTENTS holds the contents of the headline. INFO is a plist
444 used as a communication channel."
445 (let* ((column-width (org-element-property :beamer-col headline))
446 ;; Environment defaults to "block" if none is specified and
447 ;; there is no column specification. If there is a column
448 ;; specified but still no explicit environment, ENVIRONMENT
449 ;; is nil.
450 (environment (let ((env (org-element-property :beamer-env headline)))
451 (cond
452 ;; "block" is the fallback environment.
453 ((and (not env) (not column-width)) "block")
454 ;; "column" only.
455 ((not env) nil)
456 ;; Use specified environment.
457 (t (downcase env)))))
458 (env-format (when environment
459 (assoc environment
460 (append org-e-beamer-environments-special
461 org-e-beamer-environments-extra
462 org-e-beamer-environments-default))))
463 (title (org-export-data (org-element-property :title headline) info))
464 ;; Start a columns environment when there is no previous
465 ;; headline or the previous headline do not have
466 ;; a BEAMER_column property.
467 (start-columns-p
468 (and column-width
469 (or (org-export-first-sibling-p headline info)
470 (not (org-element-property
471 :beamer-col
472 (org-export-get-previous-element headline info))))))
473 ;; Ends a columns environment when there is no next headline
474 ;; or the next headline do not have a BEAMER_column property.
475 (end-columns-p
476 (and column-width
477 (or (org-export-last-sibling-p headline info)
478 (not (org-element-property
479 :beamer-col
480 (org-export-get-next-element headline info)))))))
481 (concat
482 (when start-columns-p "\\begin{columns}\n")
483 (when column-width
484 (format "\\begin{column}%s{%s}\n"
485 ;; One can specify placement for column only when
486 ;; HEADLINE stands for a column on its own.
487 (if (not environment) ""
488 (let ((options (org-element-property :beamer-opt headline)))
489 (if (not options) ""
490 (org-e-beamer--normalize-argument options 'option))))
491 (format "%s\\textwidth" column-width)))
492 ;; Block's opening string.
493 (when env-format
494 (concat
495 (org-fill-template
496 (nth 2 env-format)
497 (nconc
498 ;; If BEAMER_act property has its value enclosed in square
499 ;; brackets, it is a default overlay specification and
500 ;; overlay specification is empty. Otherwise, it is an
501 ;; overlay specification and the default one is nil.
502 (let ((action (org-element-property :beamer-act headline)))
503 (cond
504 ((not action) (list (cons "a" "") (cons "A" "")))
505 ((string-match "\\`\\[.*\\]\\'" action)
506 (list
507 (cons "A"
508 (org-e-beamer--normalize-argument action 'defaction))
509 (cons "a" "")))
511 (list
512 (cons "a"
513 (org-e-beamer--normalize-argument action 'action))
514 (cons "A" "")))))
515 (list (cons "o"
516 (let ((options
517 (org-element-property :beamer-opt headline)))
518 (if (not options) ""
519 (org-e-beamer--normalize-argument options 'option))))
520 (cons "h" title)
521 (cons "H" (if (equal title "") "" (format "{%s}" title)))
522 (cons "U" (if (equal title "") "" (format "[%s]" title))))))
523 "\n"))
524 contents
525 ;; Block's closing string.
526 (when environment (concat (nth 3 env-format) "\n"))
527 (when column-width "\\end{column}\n")
528 (when end-columns-p "\\end{columns}"))))
530 (defun org-e-beamer-headline (headline contents info)
531 "Transcode HEADLINE element into Beamer code.
532 CONTENTS is the contents of the headline. INFO is a plist used
533 as a communication channel."
534 (unless (org-element-property :footnote-section-p headline)
535 (let ((level (org-export-get-relative-level headline info))
536 (frame-level (org-e-beamer--frame-level headline info))
537 (environment (let ((env (org-element-property :beamer-env headline)))
538 (if (stringp env) (downcase env) "block"))))
539 (cond
540 ;; Creation of an appendix is requested.
541 ((equal environment "appendix")
542 (concat "\\appendix"
543 (org-element-property :beamer-act headline)
544 "\n"
545 (make-string (org-element-property :pre-blank headline) ?\n)
546 contents))
547 ((equal environment "ignoreheading")
548 (concat (make-string (org-element-property :pre-blank headline) ?\n)
549 contents))
550 ;; HEADLINE is a note.
551 ((member environment '("note" "noteNH"))
552 (format "\\note{%s}"
553 (concat (and (equal environment "note")
554 (concat
555 (org-export-data
556 (org-element-property :title headline) info)
557 "\n"))
558 (org-trim contents))))
559 ;; HEADLINE is a frame.
560 ((or (equal environment "frame") (= level frame-level))
561 (org-e-beamer--format-frame headline contents info))
562 ;; Regular section, extracted from `org-e-latex-classes'.
563 ((< level frame-level)
564 (org-e-beamer--format-section headline contents info))
565 ;; Otherwise, HEADLINE is a block.
566 (t (org-e-beamer--format-block headline contents info))))))
569 ;;;; Item
571 (defun org-e-beamer-item (item contents info)
572 "Transcode an ITEM element into Beamer code.
573 CONTENTS holds the contents of the item. INFO is a plist holding
574 contextual information."
575 (let ((action (let ((first-element (car (org-element-contents item))))
576 (and (eq (org-element-type first-element) 'paragraph)
577 (org-e-beamer--element-has-overlay-p first-element))))
578 (output (funcall (cdr (assq 'item org-e-latex-translate-alist))
579 item contents info)))
580 (if (not action) output
581 ;; If the item starts with a paragraph and that paragraph starts
582 ;; with an export snippet specifying an overlay, insert it after
583 ;; \item command.
584 (replace-regexp-in-string "\\\\item" (concat "\\\\item" action) output))))
587 ;;;; Keyword
589 (defun org-e-beamer-keyword (keyword contents info)
590 "Transcode a KEYWORD element into Beamer code.
591 CONTENTS is nil. INFO is a plist used as a communication
592 channel."
593 (let ((key (org-element-property :key keyword))
594 (value (org-element-property :value keyword)))
595 ;; Handle specifically BEAMER and TOC (headlines only) keywords.
596 ;; Otherwise, fallback to `e-latex' back-end.
597 (cond
598 ((equal key "BEAMER") value)
599 ((and (equal key "TOC") (string-match "\\<headlines\\>" value))
600 (let ((depth (or (and (string-match "[0-9]+" value)
601 (string-to-number (match-string 0 value)))
602 (plist-get info :with-toc)))
603 (options (and (string-match "\\[.*?\\]" value)
604 (match-string 0 value))))
605 (concat
606 "\\begin{frame}"
607 (when (wholenump depth) (format "\\setcounter{tocdepth}{%s}\n" depth))
608 "\\tableofcontents" options "\n"
609 "\\end{frame}")))
610 (t (funcall (cdr (assq 'keyword org-e-latex-translate-alist))
611 keyword contents info)))))
614 ;;;; Link
616 (defun org-e-beamer-link (link contents info)
617 "Transcode a LINK object into Beamer code.
618 CONTENTS is the description part of the link. INFO is a plist
619 used as a communication channel."
620 (let ((type (org-element-property :type link))
621 (path (org-element-property :path link)))
622 ;; Use \hyperlink command for all internal links.
623 (cond
624 ((equal type "radio")
625 (let ((destination (org-export-resolve-radio-link link info)))
626 (when destination
627 (format "\\hyperlink%s{%s}{%s}"
628 (or (org-e-beamer--element-has-overlay-p link) "")
629 (org-export-solidify-link-text path)
630 (org-export-data (org-element-contents destination) info)))))
631 ((and (member type '("custom-id" "fuzzy" "id"))
632 (let ((destination (if (string= type "fuzzy")
633 (org-export-resolve-fuzzy-link link info)
634 (org-export-resolve-id-link link info))))
635 (case (org-element-type destination)
636 (headline
637 (let ((label
638 (format "sec-%s"
639 (mapconcat
640 'number-to-string
641 (org-export-get-headline-number
642 destination info)
643 "-"))))
644 (if (and (plist-get info :section-numbers) (not contents))
645 (format "\\ref{%s}" label)
646 (format "\\hyperlink%s{%s}{%s}"
647 (or (org-e-beamer--element-has-overlay-p link) "")
648 label
649 contents))))
650 (target
651 (let ((path (org-export-solidify-link-text path)))
652 (if (not contents) (format "\\ref{%s}" path)
653 (format "\\hyperlink%s{%s}{%s}"
654 (or (org-e-beamer--element-has-overlay-p link) "")
655 path
656 contents))))))))
657 ;; Otherwise, use `e-latex' back-end.
658 (t (funcall (cdr (assq 'link org-e-latex-translate-alist))
659 link contents info)))))
662 ;;;; Plain List
664 ;; Plain lists support `:overlay' (for any type), `:template' (for
665 ;; ordered lists only) and `:long-text' (for description lists only)
666 ;; attributes.
668 (defun org-e-beamer-plain-list (plain-list contents info)
669 "Transcode a PLAIN-LIST element into Beamer code.
670 CONTENTS is the contents of the list. INFO is a plist holding
671 contextual information."
672 (let* ((type (org-element-property :type plain-list))
673 (attributes (org-export-read-attribute :attr_beamer plain-list))
674 (latex-type (cond ((eq type 'ordered) "enumerate")
675 ((eq type 'descriptive) "description")
676 (t "itemize"))))
677 (org-e-latex--wrap-label
678 plain-list
679 (format "\\begin{%s}%s%s\n%s\\end{%s}"
680 latex-type
681 ;; Default overlay specification, if any.
682 (let ((overlay (plist-get attributes :overlay)))
683 (if (not overlay) ""
684 (org-e-beamer--normalize-argument overlay 'defaction)))
685 ;; Second optional argument depends on the list type.
686 (case type
687 (ordered
688 (let ((template (plist-get attributes :template)))
689 (if (not template) ""
690 (org-e-beamer--normalize-argument template 'option))))
691 (descriptive
692 (let ((long-text (plist-get attributes :long-text)))
693 (if (not long-text) ""
694 (org-e-beamer--normalize-argument long-text 'option))))
695 ;; There's no second argument for un-ordered lists.
696 (otherwise ""))
697 ;; Eventually insert contents and close environment.
698 contents
699 latex-type))))
702 ;;;; Radio Target
704 (defun org-e-beamer-radio-target (radio-target text info)
705 "Transcode a RADIO-TARGET object into Beamer code.
706 TEXT is the text of the target. INFO is a plist holding
707 contextual information."
708 (format "\\hypertarget%s{%s}{%s}"
709 (or (org-e-beamer--element-has-overlay-p radio-target) "")
710 (org-export-solidify-link-text
711 (org-element-property :value radio-target))
712 text))
715 ;;;; Target
717 (defun org-e-beamer-target (target contents info)
718 "Transcode a TARGET object into Beamer code.
719 CONTENTS is nil. INFO is a plist holding contextual
720 information."
721 (format "\\hypertarget{%s}{}"
722 (org-export-solidify-link-text (org-element-property :value target))))
725 ;;;; Template
727 ;; Template used is similar to the one used in `e-latex' back-end,
728 ;; excepted for the table of contents and Beamer themes.
730 (defun org-e-beamer-template (contents info)
731 "Return complete document string after Beamer conversion.
732 CONTENTS is the transcoded contents string. INFO is a plist
733 holding export options."
734 (let ((title (org-export-data (plist-get info :title) info)))
735 (concat
736 ;; 1. Time-stamp.
737 (and (plist-get info :time-stamp-file)
738 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
739 ;; 2. Document class and packages.
740 (let ((class (plist-get info :latex-class))
741 (class-options (plist-get info :latex-class-options)))
742 (org-element-normalize-string
743 (let* ((header (nth 1 (assoc class org-e-latex-classes)))
744 (document-class-string
745 (and (stringp header)
746 (if class-options
747 (replace-regexp-in-string
748 "^[ \t]*\\\\documentclass\\(\\[.*?\\]\\)"
749 class-options header t nil 1)
750 header))))
751 (when document-class-string
752 (org-e-latex--guess-babel-language
753 (org-e-latex--guess-inputenc
754 (org-splice-latex-header
755 document-class-string
756 org-export-latex-default-packages-alist ; defined in org.el
757 org-export-latex-packages-alist nil ; defined in org.el
758 (plist-get info :latex-header-extra)))
759 info)))))
760 ;; 3. Insert themes.
761 (let ((format-theme
762 (function
763 (lambda (prop command)
764 (let ((theme (plist-get info prop)))
765 (when theme
766 (concat command
767 (if (not (string-match "\\[.*\\]" theme))
768 (format "{%s}\n" theme)
769 (format "%s{%s}\n"
770 (match-string 0 theme)
771 (org-trim
772 (replace-match "" nil nil theme)))))))))))
773 (mapconcat (lambda (args) (apply format-theme args))
774 '((:beamer-theme "\\usetheme")
775 (:beamer-color-theme "\\usecolortheme")
776 (:beamer-font-theme "\\usefonttheme")
777 (:beamer-inner-theme "\\useinnertheme")
778 (:beamer-outer-theme "\\useoutertheme"))
779 ""))
780 ;; 4. Possibly limit depth for headline numbering.
781 (let ((sec-num (plist-get info :section-numbers)))
782 (when (integerp sec-num)
783 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
784 ;; 5. Author.
785 (let ((author (and (plist-get info :with-author)
786 (let ((auth (plist-get info :author)))
787 (and auth (org-export-data auth info)))))
788 (email (and (plist-get info :with-email)
789 (org-export-data (plist-get info :email) info))))
790 (cond ((and author email (not (string= "" email)))
791 (format "\\author{%s\\thanks{%s}}\n" author email))
792 (author (format "\\author{%s}\n" author))
793 (t "\\author{}\n")))
794 ;; 6. Date.
795 (format "\\date{%s}\n" (org-export-data (plist-get info :date) info))
796 ;; 7. Title
797 (format "\\title{%s}\n" title)
798 ;; 8. Hyperref options.
799 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
800 (or (plist-get info :keywords) "")
801 (or (plist-get info :description) "")
802 (if (not (plist-get info :with-creator)) ""
803 (plist-get info :creator)))
804 ;; 9. Document start.
805 "\\begin{document}\n\n"
806 ;; 10. Title command.
807 (org-element-normalize-string
808 (cond ((string= "" title) nil)
809 ((not (stringp org-e-latex-title-command)) nil)
810 ((string-match "\\(?:[^%]\\|^\\)%s"
811 org-e-latex-title-command)
812 (format org-e-latex-title-command title))
813 (t org-e-latex-title-command)))
814 ;; 11. Table of contents.
815 (let ((depth (plist-get info :with-toc)))
816 (when depth
817 (concat
818 (format "\\begin{frame}%s{%s}\n"
819 (org-e-beamer--normalize-argument
820 org-e-beamer-outline-frame-options 'option)
821 org-e-beamer-outline-frame-title)
822 (when (wholenump depth)
823 (format "\\setcounter{tocdepth}{%d}\n" depth))
824 "\\tableofcontents\n"
825 "\\end{frame}\n\n")))
826 ;; 12. Document's body.
827 contents
828 ;; 13. Creator.
829 (let ((creator-info (plist-get info :with-creator)))
830 (cond
831 ((not creator-info) "")
832 ((eq creator-info 'comment)
833 (format "%% %s\n" (plist-get info :creator)))
834 (t (concat (plist-get info :creator) "\n"))))
835 ;; 14. Document end.
836 "\\end{document}")))
840 ;;; Minor Mode
843 (defvar org-e-beamer-mode-map (make-sparse-keymap)
844 "The keymap for `org-e-beamer-mode'.")
845 (define-key org-e-beamer-mode-map "\C-c\C-b" 'org-e-beamer-select-environment)
847 ;;;###autoload
848 (define-minor-mode org-e-beamer-mode
849 "Support for editing Beamer oriented Org mode files."
850 nil " Bm" 'org-e-beamer-mode-map)
852 (when (fboundp 'font-lock-add-keywords)
853 (font-lock-add-keywords
854 'org-mode
855 '((":\\(B_[a-z]+\\|BMCOL\\):" 1 'org-e-beamer-tag prepend))
856 'prepend))
858 (defface org-e-beamer-tag '((t (:box (:line-width 1 :color grey40))))
859 "The special face for beamer tags."
860 :group 'org-export-e-beamer)
862 (defun org-e-beamer-property-changed (property value)
863 "Track the BEAMER_env property with tags.
864 PROPERTY is the name of the modified property. VALUE is its new
865 value."
866 (cond
867 ((equal property "BEAMER_env")
868 (save-excursion
869 (org-back-to-heading t)
870 (let ((tags (org-get-tags)))
871 (setq tags (delq nil (mapcar (lambda (x)
872 (if (string-match "^B_" x) nil x))
873 tags)))
874 (org-set-tags-to tags))
875 (when (org-string-nw-p value) (org-toggle-tag (concat "B_" value) 'on))))
876 ((equal property "BEAMER_col")
877 (org-toggle-tag "BMCOL" (if (org-string-nw-p value) 'on 'off)))))
879 (add-hook 'org-property-changed-functions 'org-e-beamer-property-changed)
881 (defun org-e-beamer-allowed-property-values (property)
882 "Supply allowed values for PROPERTY."
883 (cond
884 ((and (equal property "BEAMER_env")
885 (not (org-entry-get nil (concat property "_ALL") 'inherit)))
886 ;; If no allowed values for BEAMER_env have been defined,
887 ;; supply all defined environments
888 (mapcar 'car (append org-e-beamer-environments-special
889 org-e-beamer-environments-extra
890 org-e-beamer-environments-default)))
891 ((and (equal property "BEAMER_col")
892 (not (org-entry-get nil (concat property "_ALL") 'inherit)))
893 ;; If no allowed values for BEAMER_col have been defined,
894 ;; supply some
895 (org-split-string org-e-beamer-column-widths " "))))
897 (add-hook 'org-property-allowed-value-functions
898 'org-e-beamer-allowed-property-values)
902 ;;; Commands
904 ;;;###autoload
905 (defun org-e-beamer-export-as-latex
906 (&optional subtreep visible-only body-only ext-plist)
907 "Export current buffer as a Beamer buffer.
909 If narrowing is active in the current buffer, only export its
910 narrowed part.
912 If a region is active, export that region.
914 When optional argument SUBTREEP is non-nil, export the sub-tree
915 at point, extracting information from the headline properties
916 first.
918 When optional argument VISIBLE-ONLY is non-nil, don't export
919 contents of hidden elements.
921 When optional argument BODY-ONLY is non-nil, only write code
922 between \"\\begin{document}\" and \"\\end{document}\".
924 EXT-PLIST, when provided, is a property list with external
925 parameters overriding Org default settings, but still inferior to
926 file-local settings.
928 Export is done in a buffer named \"*Org E-BEAMER Export*\", which
929 will be displayed when `org-export-show-temporary-export-buffer'
930 is non-nil."
931 (interactive)
932 (let ((outbuf (org-export-to-buffer
933 'e-beamer "*Org E-BEAMER Export*"
934 subtreep visible-only body-only ext-plist)))
935 (with-current-buffer outbuf (LaTeX-mode))
936 (when org-export-show-temporary-export-buffer
937 (switch-to-buffer-other-window outbuf))))
939 ;;;###autoload
940 (defun org-e-beamer-export-to-latex
941 (&optional subtreep visible-only body-only ext-plist pub-dir)
942 "Export current buffer as a Beamer presentation (tex).
944 If narrowing is active in the current buffer, only export its
945 narrowed part.
947 If a region is active, export that region.
949 When optional argument SUBTREEP is non-nil, export the sub-tree
950 at point, extracting information from the headline properties
951 first.
953 When optional argument VISIBLE-ONLY is non-nil, don't export
954 contents of hidden elements.
956 When optional argument BODY-ONLY is non-nil, only write code
957 between \"\\begin{document}\" and \"\\end{document}\".
959 EXT-PLIST, when provided, is a property list with external
960 parameters overriding Org default settings, but still inferior to
961 file-local settings.
963 When optional argument PUB-DIR is set, use it as the publishing
964 directory.
966 Return output file's name."
967 (interactive)
968 (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir)))
969 (org-export-to-file
970 'e-beamer outfile subtreep visible-only body-only ext-plist)))
972 ;;;###autoload
973 (defun org-e-beamer-export-to-pdf
974 (&optional subtreep visible-only body-only ext-plist pub-dir)
975 "Export current buffer as a Beamer presentation (PDF).
977 If narrowing is active in the current buffer, only export its
978 narrowed part.
980 If a region is active, export that region.
982 When optional argument SUBTREEP is non-nil, export the sub-tree
983 at point, extracting information from the headline properties
984 first.
986 When optional argument VISIBLE-ONLY is non-nil, don't export
987 contents of hidden elements.
989 When optional argument BODY-ONLY is non-nil, only write code
990 between \"\\begin{document}\" and \"\\end{document}\".
992 EXT-PLIST, when provided, is a property list with external
993 parameters overriding Org default settings, but still inferior to
994 file-local settings.
996 When optional argument PUB-DIR is set, use it as the publishing
997 directory.
999 Return PDF file's name."
1000 (interactive)
1001 (org-e-latex-compile
1002 (org-e-beamer-export-to-latex
1003 subtreep visible-only body-only ext-plist pub-dir)))
1005 ;;;###autoload
1006 (defun org-e-beamer-select-environment ()
1007 "Select the environment to be used by beamer for this entry.
1008 While this uses (for convenience) a tag selection interface, the
1009 result of this command will be that the BEAMER_env *property* of
1010 the entry is set.
1012 In addition to this, the command will also set a tag as a visual
1013 aid, but the tag does not have any semantic meaning."
1014 (interactive)
1015 ;; Make sure `org-e-beamer-environments-special' has a higher
1016 ;; priority than `org-e-beamer-environments-extra'.
1017 (let* ((envs (append org-e-beamer-environments-special
1018 org-e-beamer-environments-extra
1019 org-e-beamer-environments-default))
1020 (org-tag-alist
1021 (append '((:startgroup))
1022 (mapcar (lambda (e) (cons (concat "B_" (car e))
1023 (string-to-char (nth 1 e))))
1024 envs)
1025 '((:endgroup))
1026 '(("BMCOL" . ?|))))
1027 (org-fast-tag-selection-single-key t))
1028 (org-set-tags)
1029 (let ((tags (or (ignore-errors (org-get-tags-string)) "")))
1030 (cond
1031 ((eq org-last-tag-selection-key ?|)
1032 (if (string-match ":BMCOL:" tags)
1033 (org-set-property "BEAMER_col" (read-string "Column width: "))
1034 (org-delete-property "BEAMER_col")))
1035 ((string-match (concat ":B_\\("
1036 (mapconcat 'car envs "\\|")
1037 "\\):")
1038 tags)
1039 (org-entry-put nil "BEAMER_env" (match-string 1 tags)))
1040 (t (org-entry-delete nil "BEAMER_env"))))))
1042 ;;;###autoload
1043 (defun org-e-beamer-insert-options-template (&optional kind)
1044 "Insert a settings template, to make sure users do this right."
1045 (interactive (progn
1046 (message "Current [s]ubtree or [g]lobal?")
1047 (if (eq (read-char-exclusive) ?g) (list 'global)
1048 (list 'subtree))))
1049 (if (eq kind 'subtree)
1050 (progn
1051 (org-back-to-heading t)
1052 (org-reveal)
1053 (org-entry-put nil "EXPORT_LaTeX_CLASS" "beamer")
1054 (org-entry-put nil "EXPORT_LaTeX_CLASS_OPTIONS" "[presentation]")
1055 (org-entry-put nil "EXPORT_FILE_NAME" "presentation.pdf")
1056 (when org-e-beamer-column-view-format
1057 (org-entry-put nil "COLUMNS" org-e-beamer-column-view-format))
1058 (org-entry-put nil "BEAMER_col_ALL" org-e-beamer-column-widths))
1059 (insert "#+LaTeX_CLASS: beamer\n")
1060 (insert "#+LaTeX_CLASS_OPTIONS: [presentation]\n")
1061 (when org-e-beamer-theme
1062 (insert "#+BEAMER_THEME: " org-e-beamer-theme "\n"))
1063 (when org-e-beamer-column-view-format
1064 (insert "#+COLUMNS: " org-e-beamer-column-view-format "\n"))
1065 (insert "#+PROPERTY: BEAMER_col_ALL " org-e-beamer-column-widths "\n")))
1068 (provide 'org-e-beamer)
1069 ;;; org-e-beamer.el ends here