org-e-beamer: Fix customize group and a variable name
[org-mode.git] / contrib / lisp / org-e-beamer.el
blobda305f85de07dca4fd5916367301e581edf3042a
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-e-beamer
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 nil
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 (case type
231 (action (if (string-match "\\`<.*>\\'" argument) argument
232 (format "<%s>" argument)))
233 (defaction (cond
234 ((string-match "\\`\\[<.*>\\]\\'" argument) argument)
235 ((string-match "\\`<.*>\\'" argument)
236 (format "[%s]" argument))
237 ((string-match "\\`\\[\\(.*\\)\\]\\'" argument)
238 (format "[<%s>]" (match-string 1 argument)))
239 (t (format "[<%s>]" argument))))
240 (option (if (string-match "\\`\\[.*\\]\\'" argument) argument
241 (format "[%s]" argument)))
242 (otherwise argument)))
244 (defun org-e-beamer--element-has-overlay-p (element)
245 "Non-nil when ELEMENT has an overlay specified.
246 An element has an overlay specification when it starts with an
247 `e-beamer' export-snippet whose value is between angular
248 brackets. Return overlay specification, as a string, or nil."
249 (let ((first-object (car (org-element-contents element))))
250 (when (eq (org-element-type first-object) 'export-snippet)
251 (let ((value (org-element-property :value first-object)))
252 (and (string-match "\\`<.*>\\'" value) value)))))
256 ;;; Define Back-End
258 (org-export-define-derived-backend e-beamer e-latex
259 :options-alist
260 ((:beamer-theme "BEAMER_THEME" nil org-e-beamer-theme)
261 (:beamer-color-theme "BEAMER_COLOR_THEME" nil nil t)
262 (:beamer-font-theme "BEAMER_FONT_THEME" nil nil t)
263 (:beamer-inner-theme "BEAMER_INNER_THEME" nil nil t)
264 (:beamer-outer-theme "BEAMER_OUTER_THEME" nil nil t)
265 (:headline-levels nil "H" org-e-beamer-frame-level))
266 :translate-alist ((bold . org-e-beamer-bold)
267 (export-block . org-e-beamer-export-block)
268 (export-snippet . org-e-beamer-export-snippet)
269 (headline . org-e-beamer-headline)
270 (item . org-e-beamer-item)
271 (keyword . org-e-beamer-keyword)
272 (link . org-e-beamer-link)
273 (plain-list . org-e-beamer-plain-list)
274 (radio-target . org-e-beamer-radio-target)
275 (target . org-e-beamer-target)
276 (template . org-e-beamer-template)))
280 ;;; Transcode Functions
282 ;;;; Bold
284 (defun org-e-beamer-bold (bold contents info)
285 "Transcode BLOCK object into Beamer code.
286 CONTENTS is the text being bold. INFO is a plist used as
287 a communication channel."
288 (format "\\alert%s{%s}"
289 (or (org-e-beamer--element-has-overlay-p bold) "")
290 contents))
293 ;;;; Export Block
295 (defun org-e-beamer-export-block (export-block contents info)
296 "Transcode an EXPORT-BLOCK element into Beamer code.
297 CONTENTS is nil. INFO is a plist used as a communication
298 channel."
299 (when (member (org-element-property :type export-block) '("BEAMER" "LATEX"))
300 (org-remove-indentation (org-element-property :value export-block))))
303 ;;;; Export Snippet
305 (defun org-e-beamer-export-snippet (export-snippet contents info)
306 "Transcode an EXPORT-SNIPPET object into Beamer code.
307 CONTENTS is nil. INFO is a plist used as a communication
308 channel."
309 (let ((backend (org-export-snippet-backend export-snippet))
310 (value (org-element-property :value export-snippet)))
311 ;; Only "e-latex" and "e-beamer" snippets are retained.
312 (cond ((eq backend 'e-latex) value)
313 ;; Ignore "e-beamer" snippets specifying overlays.
314 ((and (eq backend 'e-beamer)
315 (or (org-export-get-previous-element export-snippet)
316 (not (string-match "\\`<.*>\\'" value))))
317 value))))
320 ;;;; Headline
322 ;; The main function to translate an headline is
323 ;; `org-e-beamer-headline'.
325 ;; Depending on the level at which an headline is considered as
326 ;; a frame (given by `org-e-beamer--frame-level'), the headline is
327 ;; either a section (`org-e-beamer--format-section'), a frame
328 ;; (`org-e-beamer--format-frame') or a block
329 ;; (`org-e-beamer--format-block').
331 ;; `org-e-beamer-headline' also takes care of special environments
332 ;; like "ignoreheading", "note", "noteNH" and "appendix".
334 (defun org-e-beamer--frame-level (headline info)
335 "Return frame level in subtree containing HEADLINE.
336 INFO is a plist used as a communication channel."
338 ;; 1. Look for "frame" environment in parents, starting from the
339 ;; farthest.
340 (catch 'exit
341 (mapc (lambda (parent)
342 (when (equal (org-element-property :beamer-env parent) "frame")
343 (throw 'exit (org-export-get-relative-level parent info))))
344 (reverse (org-export-get-genealogy headline)))
345 nil)
346 ;; 2. Look for "frame" environment in HEADLINE.
347 (and (equal (org-element-property :beamer-env headline) "frame")
348 (org-export-get-relative-level headline info))
349 ;; 3. Look for "frame" environment in sub-tree.
350 (org-element-map
351 headline 'headline
352 (lambda (hl)
353 (when (equal (org-element-property :beamer-env hl) "frame")
354 (org-export-get-relative-level hl info)))
355 info 'first-match)
356 ;; 4. No "frame" environment in tree: use default value.
357 (plist-get info :headline-levels)))
359 (defun org-e-beamer--format-section (headline contents info)
360 "Format HEADLINE as a sectioning part.
361 CONTENTS holds the contents of the headline. INFO is a plist
362 used as a communication channel."
363 ;; Use `e-latex' back-end output, inserting overlay specifications
364 ;; if possible.
365 (let ((latex-headline
366 (funcall (cdr (assq 'headline org-e-latex-translate-alist))
367 headline contents info))
368 (mode-specs (org-element-property :beamer-act headline)))
369 (if (and mode-specs
370 (string-match "\\`\\\\\\(.*?\\)\\(?:\\*\\|\\[.*\\]\\)?{"
371 latex-headline))
372 (replace-match (concat (match-string 1 latex-headline)
373 (format "<%s>" mode-specs))
374 nil nil latex-headline 1)
375 latex-headline)))
377 (defun org-e-beamer--format-frame (headline contents info)
378 "Format HEADLINE as a frame.
379 CONTENTS holds the contents of the headline. INFO is a plist
380 used as a communication channel."
381 (concat "\\begin{frame}"
382 ;; Overlay specification, if any. If is surrounded by square
383 ;; brackets, consider it as a default specification.
384 (let ((action (org-element-property :beamer-act headline)))
385 (cond
386 ((not action) "")
387 ((string-match "\\`\\[.*\\]\\'" action )
388 (org-e-beamer--normalize-argument action 'defaction))
389 (t (org-e-beamer--normalize-argument action 'action))))
390 ;; Options, if any.
391 (let ((options
392 ;; Collect options from default value and headline's
393 ;; properties. Also add a label for links.
394 (append
395 (org-split-string org-e-beamer-frame-default-options
396 ",")
397 (let ((opt (org-element-property :beamer-opt headline)))
398 (and opt (org-split-string
399 ;; Remove square brackets if user
400 ;; provided them.
401 (and (string-match "^\\[?\\(.*\\)\\]?$" opt)
402 (match-string 1 opt))
403 ",")))
404 (list
405 (format "label=sec-%s"
406 (mapconcat
407 'number-to-string
408 (org-export-get-headline-number headline info)
409 "-"))))))
410 ;; Change options list into a string. FRAGILEP is non-nil
411 ;; when HEADLINE contains an element among
412 ;; `org-e-beamer-verbatim-elements'.
413 (let ((fragilep (org-element-map
414 headline org-e-beamer-verbatim-elements 'identity
415 info 'first-match)))
416 (org-e-beamer--normalize-argument
417 (mapconcat
418 'identity
419 (if (or (not fragilep) (member "fragile" options)) options
420 (cons "fragile" options))
421 ",")
422 'option)))
423 ;; Title.
424 (format "{%s}"
425 (org-export-data (org-element-property :title headline)
426 info))
427 "\n"
428 contents
429 "\\end{frame}"))
431 (defun org-e-beamer--format-block (headline contents info)
432 "Format HEADLINE as a block.
433 CONTENTS holds the contents of the headline. INFO is a plist
434 used as a communication channel."
435 (let* ((column-width (org-element-property :beamer-col headline))
436 ;; Environment defaults to "block" if none is specified and
437 ;; there is no column specification. If there is a column
438 ;; specified but still no explicit environment, ENVIRONMENT
439 ;; is nil.
440 (environment (let ((env (org-element-property :beamer-env headline)))
441 (cond
442 ;; "block" is the fallback environment.
443 ((and (not env) (not column-width)) "block")
444 ;; "column" only.
445 ((not env) nil)
446 ;; Use specified environment.
447 (t (downcase env)))))
448 (env-format (when environment
449 (assoc environment
450 (append org-e-beamer-environments-special
451 org-e-beamer-environments-extra
452 org-e-beamer-environments-default))))
453 (title (org-export-data (org-element-property :title headline) info))
454 ;; Start a columns environment when there is no previous
455 ;; headline or the previous headline do not have
456 ;; a BEAMER_column property.
457 (start-columns-p
458 (and column-width
459 (or (org-export-first-sibling-p headline)
460 (not (org-element-property
461 :beamer-col
462 (org-export-get-previous-element headline))))))
463 ;; Ends a columns environment when there is no next headline
464 ;; or the next headline do not have a BEAMER_column property.
465 (end-columns-p
466 (and column-width
467 (or (org-export-last-sibling-p headline)
468 (not (org-element-property
469 :beamer-col
470 (org-export-get-next-element headline)))))))
471 (concat
472 (when start-columns-p "\\begin{columns}\n")
473 (when column-width
474 (format "\\begin{column}%s{%s}\n"
475 ;; One can specify placement for column only when
476 ;; HEADLINE stands for a column on its own.
477 (if (not environment) ""
478 (let ((options (org-element-property :beamer-opt headline)))
479 (if (not options) ""
480 (org-e-beamer--normalize-argument options 'option))))
481 (format "%s\\textwidth" column-width)))
482 ;; Block's opening string.
483 (when env-format
484 (concat
485 (org-fill-template
486 (nth 2 env-format)
487 (nconc
488 ;; If BEAMER_act property has its value enclosed in square
489 ;; brackets, it is a default overlay specification and
490 ;; overlay specification is empty. Otherwise, it is an
491 ;; overlay specification and the default one is nil.
492 (let ((action (org-element-property :beamer-act headline)))
493 (cond
494 ((not action) (list (cons "a" "") (cons "A" "")))
495 ((string-match "\\`\\[.*\\]\\'" action)
496 (list
497 (cons "A"
498 (org-e-beamer--normalize-argument action 'defaction))
499 (cons "a" "")))
501 (list
502 (cons "a"
503 (org-e-beamer--normalize-argument action 'action))
504 (cons "A" "")))))
505 (list (cons "o"
506 (let ((options
507 (org-element-property :beamer-opt headline)))
508 (if (not options) ""
509 (org-e-beamer--normalize-argument options 'option))))
510 (cons "h" title)
511 (cons "H" (if (equal title "") "" (format "{%s}" title)))
512 (cons "U" (if (equal title "") "" (format "[%s]" title))))))
513 "\n"))
514 contents
515 ;; Block's closing string.
516 (when environment (concat (nth 3 env-format) "\n"))
517 (when column-width "\\end{column}\n")
518 (when end-columns-p "\\end{columns}"))))
520 (defun org-e-beamer-headline (headline contents info)
521 "Transcode HEADLINE element into Beamer code.
522 CONTENTS is the contents of the headline. INFO is a plist used
523 as a communication channel."
524 (unless (org-element-property :footnote-section-p headline)
525 (let ((level (org-export-get-relative-level headline info))
526 (frame-level (org-e-beamer--frame-level headline info))
527 (environment (let ((env (org-element-property :beamer-env headline)))
528 (if (stringp env) (downcase env) "block"))))
529 (cond
530 ;; Creation of an appendix is requested.
531 ((equal environment "appendix")
532 (concat "\\appendix"
533 (org-element-property :beamer-act headline)
534 "\n"
535 (make-string (org-element-property :pre-blank headline) ?\n)
536 contents))
537 ((equal environment "ignoreheading")
538 (concat (make-string (org-element-property :pre-blank headline) ?\n)
539 contents))
540 ;; HEADLINE is a note.
541 ((member environment '("note" "noteNH"))
542 (format "\\note{%s}"
543 (concat (and (equal environment "note")
544 (concat
545 (org-export-data
546 (org-element-property :title headline) info)
547 "\n"))
548 (org-trim contents))))
549 ;; HEADLINE is a frame.
550 ((or (equal environment "frame") (= level frame-level))
551 (org-e-beamer--format-frame headline contents info))
552 ;; Regular section, extracted from `org-e-latex-classes'.
553 ((< level frame-level)
554 (org-e-beamer--format-section headline contents info))
555 ;; Otherwise, HEADLINE is a block.
556 (t (org-e-beamer--format-block headline contents info))))))
559 ;;;; Item
561 (defun org-e-beamer-item (item contents info)
562 "Transcode an ITEM element into Beamer code.
563 CONTENTS holds the contents of the item. INFO is a plist holding
564 contextual information."
565 (let ((action (let ((first-element (car (org-element-contents item))))
566 (and (eq (org-element-type first-element) 'paragraph)
567 (org-e-beamer--element-has-overlay-p first-element))))
568 (output (funcall (cdr (assq 'item org-e-latex-translate-alist))
569 item contents info)))
570 (if (not action) output
571 ;; If the item starts with a paragraph and that paragraph starts
572 ;; with an export snippet specifying an overlay, insert it after
573 ;; \item command.
574 (replace-regexp-in-string "\\\\item" (concat "\\\\item" action) output))))
577 ;;;; Keyword
579 (defun org-e-beamer-keyword (keyword contents info)
580 "Transcode a KEYWORD element into Beamer code.
581 CONTENTS is nil. INFO is a plist used as a communication
582 channel."
583 (let ((key (org-element-property :key keyword))
584 (value (org-element-property :value keyword)))
585 ;; Handle specifically BEAMER and TOC (headlines only) keywords.
586 ;; Otherwise, fallback to `e-latex' back-end.
587 (cond
588 ((equal key "BEAMER") value)
589 ((and (equal key "TOC") (string-match "\\<headlines\\>" value))
590 (let ((depth (or (and (string-match "[0-9]+" value)
591 (string-to-number (match-string 0 value)))
592 (plist-get info :with-toc)))
593 (options (and (string-match "\\[.*?\\]" value)
594 (match-string 0 value))))
595 (concat
596 "\\begin{frame}"
597 (when (wholenump depth) (format "\\setcounter{tocdepth}{%s}\n" depth))
598 "\\tableofcontents" options "\n"
599 "\\end{frame}")))
600 (t (funcall (cdr (assq 'keyword org-e-latex-translate-alist))
601 keyword contents info)))))
604 ;;;; Link
606 (defun org-e-beamer-link (link contents info)
607 "Transcode a LINK object into Beamer code.
608 CONTENTS is the description part of the link. INFO is a plist
609 used as a communication channel."
610 (let ((type (org-element-property :type link))
611 (path (org-element-property :path link)))
612 ;; Use \hyperlink command for all internal links.
613 (cond
614 ((equal type "radio")
615 (let ((destination (org-export-resolve-radio-link link info)))
616 (when destination
617 (format "\\hyperlink%s{%s}{%s}"
618 (or (org-e-beamer--element-has-overlay-p link) "")
619 (org-export-solidify-link-text path)
620 (org-export-data (org-element-contents destination) info)))))
621 ((and (member type '("custom-id" "fuzzy" "id"))
622 (let ((destination (if (string= type "fuzzy")
623 (org-export-resolve-fuzzy-link link info)
624 (org-export-resolve-id-link link info))))
625 (case (org-element-type destination)
626 (headline
627 (let ((label
628 (format "sec-%s"
629 (mapconcat
630 'number-to-string
631 (org-export-get-headline-number
632 destination info)
633 "-"))))
634 (if (and (plist-get info :section-numbers) (not contents))
635 (format "\\ref{%s}" label)
636 (format "\\hyperlink%s{%s}{%s}"
637 (or (org-e-beamer--element-has-overlay-p link) "")
638 label
639 contents))))
640 (target
641 (let ((path (org-export-solidify-link-text path)))
642 (if (not contents) (format "\\ref{%s}" path)
643 (format "\\hyperlink%s{%s}{%s}"
644 (or (org-e-beamer--element-has-overlay-p link) "")
645 path
646 contents))))))))
647 ;; Otherwise, use `e-latex' back-end.
648 (t (funcall (cdr (assq 'link org-e-latex-translate-alist))
649 link contents info)))))
652 ;;;; Plain List
654 ;; Plain lists support `:overlay' (for any type), `:template' (for
655 ;; ordered lists only) and `:long-text' (for description lists only)
656 ;; attributes.
658 (defun org-e-beamer-plain-list (plain-list contents info)
659 "Transcode a PLAIN-LIST element into Beamer code.
660 CONTENTS is the contents of the list. INFO is a plist holding
661 contextual information."
662 (let* ((type (org-element-property :type plain-list))
663 (attributes (org-export-read-attribute :attr_beamer plain-list))
664 (latex-type (cond ((eq type 'ordered) "enumerate")
665 ((eq type 'descriptive) "description")
666 (t "itemize"))))
667 (org-e-latex--wrap-label
668 plain-list
669 (format "\\begin{%s}%s%s\n%s\\end{%s}"
670 latex-type
671 ;; Default overlay specification, if any.
672 (let ((overlay (plist-get attributes :overlay)))
673 (if (not overlay) ""
674 (org-e-beamer--normalize-argument overlay 'defaction)))
675 ;; Second optional argument depends on the list type.
676 (case type
677 (ordered
678 (let ((template (plist-get attributes :template)))
679 (if (not template) ""
680 (org-e-beamer--normalize-argument template 'option))))
681 (descriptive
682 (let ((long-text (plist-get attributes :long-text)))
683 (if (not long-text) ""
684 (org-e-beamer--normalize-argument long-text 'option))))
685 ;; There's no second argument for un-ordered lists.
686 (otherwise ""))
687 ;; Eventually insert contents and close environment.
688 contents
689 latex-type))))
692 ;;;; Radio Target
694 (defun org-e-beamer-radio-target (radio-target text info)
695 "Transcode a RADIO-TARGET object into Beamer code.
696 TEXT is the text of the target. INFO is a plist holding
697 contextual information."
698 (format "\\hypertarget%s{%s}{%s}"
699 (or (org-e-beamer--element-has-overlay-p radio-target) "")
700 (org-export-solidify-link-text
701 (org-element-property :value radio-target))
702 text))
705 ;;;; Target
707 (defun org-e-beamer-target (target contents info)
708 "Transcode a TARGET object into Beamer code.
709 CONTENTS is nil. INFO is a plist holding contextual
710 information."
711 (format "\\hypertarget{%s}{}"
712 (org-export-solidify-link-text (org-element-property :value target))))
715 ;;;; Template
717 ;; Template used is similar to the one used in `e-latex' back-end,
718 ;; excepted for the table of contents and Beamer themes.
720 (defun org-e-beamer-template (contents info)
721 "Return complete document string after Beamer conversion.
722 CONTENTS is the transcoded contents string. INFO is a plist
723 holding export options."
724 (let ((title (org-export-data (plist-get info :title) info)))
725 (concat
726 ;; 1. Time-stamp.
727 (and (plist-get info :time-stamp-file)
728 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
729 ;; 2. Document class and packages.
730 (let ((class (plist-get info :latex-class))
731 (class-options (plist-get info :latex-class-options)))
732 (org-element-normalize-string
733 (let* ((header (nth 1 (assoc class org-e-latex-classes)))
734 (document-class-string
735 (and (stringp header)
736 (if class-options
737 (replace-regexp-in-string
738 "^[ \t]*\\\\documentclass\\(\\[.*?\\]\\)"
739 class-options header t nil 1)
740 header))))
741 (when document-class-string
742 (org-e-latex--guess-babel-language
743 (org-e-latex--guess-inputenc
744 (org-splice-latex-header
745 document-class-string
746 org-export-latex-default-packages-alist ; defined in org.el
747 org-export-latex-packages-alist nil ; defined in org.el
748 (plist-get info :latex-header-extra)))
749 info)))))
750 ;; 3. Insert themes.
751 (let ((format-theme
752 (function
753 (lambda (prop command)
754 (let ((theme (plist-get info prop)))
755 (when theme
756 (concat command
757 (if (not (string-match "\\[.*\\]" theme))
758 (format "{%s}\n" theme)
759 (format "%s{%s}\n"
760 (match-string 0 theme)
761 (org-trim
762 (replace-match "" nil nil theme)))))))))))
763 (mapconcat (lambda (args) (apply format-theme args))
764 '((:beamer-theme "\\usetheme")
765 (:beamer-color-theme "\\usecolortheme")
766 (:beamer-font-theme "\\usefonttheme")
767 (:beamer-inner-theme "\\useinnertheme")
768 (:beamer-outer-theme "\\useoutertheme"))
769 ""))
770 ;; 4. Possibly limit depth for headline numbering.
771 (let ((sec-num (plist-get info :section-numbers)))
772 (when (integerp sec-num)
773 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
774 ;; 5. Author.
775 (let ((author (and (plist-get info :with-author)
776 (let ((auth (plist-get info :author)))
777 (and auth (org-export-data auth info)))))
778 (email (and (plist-get info :with-email)
779 (org-export-data (plist-get info :email) info))))
780 (cond ((and author email (not (string= "" email)))
781 (format "\\author{%s\\thanks{%s}}\n" author email))
782 (author (format "\\author{%s}\n" author))
783 (t "\\author{}\n")))
784 ;; 6. Date.
785 (format "\\date{%s}\n" (org-export-data (plist-get info :date) info))
786 ;; 7. Title
787 (format "\\title{%s}\n" title)
788 ;; 8. Hyperref options.
789 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
790 (or (plist-get info :keywords) "")
791 (or (plist-get info :description) "")
792 (if (not (plist-get info :with-creator)) ""
793 (plist-get info :creator)))
794 ;; 9. Document start.
795 "\\begin{document}\n\n"
796 ;; 10. Title command.
797 (org-element-normalize-string
798 (cond ((string= "" title) nil)
799 ((not (stringp org-e-latex-title-command)) nil)
800 ((string-match "\\(?:[^%]\\|^\\)%s"
801 org-e-latex-title-command)
802 (format org-e-latex-title-command title))
803 (t org-e-latex-title-command)))
804 ;; 11. Table of contents.
805 (let ((depth (plist-get info :with-toc)))
806 (when depth
807 (concat
808 (format "\\begin{frame}%s{%s}\n"
809 (org-e-beamer--normalize-argument
810 org-e-beamer-outline-frame-options 'option)
811 org-e-beamer-outline-frame-title)
812 (when (wholenump depth)
813 (format "\\setcounter{tocdepth}{%d}\n" depth))
814 "\\tableofcontents\n"
815 "\\end{frame}\n\n")))
816 ;; 12. Document's body.
817 contents
818 ;; 13. Creator.
819 (let ((creator-info (plist-get info :with-creator)))
820 (cond
821 ((not creator-info) "")
822 ((eq creator-info 'comment)
823 (format "%% %s\n" (plist-get info :creator)))
824 (t (concat (plist-get info :creator) "\n"))))
825 ;; 14. Document end.
826 "\\end{document}")))
830 ;;; Minor Mode
833 (defvar org-e-beamer-mode-map (make-sparse-keymap)
834 "The keymap for `org-e-beamer-mode'.")
835 (define-key org-e-beamer-mode-map "\C-c\C-b" 'org-e-beamer-select-environment)
837 ;;;###autoload
838 (define-minor-mode org-e-beamer-mode
839 "Support for editing Beamer oriented Org mode files."
840 nil " Bm" 'org-e-beamer-mode-map)
842 (when (fboundp 'font-lock-add-keywords)
843 (font-lock-add-keywords
844 'org-mode
845 '((":\\(B_[a-z]+\\|BMCOL\\):" 1 'org-e-beamer-tag prepend))
846 'prepend))
848 (defface org-e-beamer-tag '((t (:box (:line-width 1 :color grey40))))
849 "The special face for beamer tags."
850 :group 'org-export-e-beamer)
852 (defun org-e-beamer-property-changed (property value)
853 "Track the BEAMER_env property with tags.
854 PROPERTY is the name of the modified property. VALUE is its new
855 value."
856 (cond
857 ((equal property "BEAMER_env")
858 (save-excursion
859 (org-back-to-heading t)
860 (let ((tags (org-get-tags)))
861 (setq tags (delq nil (mapcar (lambda (x)
862 (if (string-match "^B_" x) nil x))
863 tags)))
864 (org-set-tags-to tags))
865 (when (org-string-nw-p value) (org-toggle-tag (concat "B_" value) 'on))))
866 ((equal property "BEAMER_col")
867 (org-toggle-tag "BMCOL" (if (org-string-nw-p value) 'on 'off)))))
869 (add-hook 'org-property-changed-functions 'org-e-beamer-property-changed)
871 (defun org-e-beamer-allowed-property-values (property)
872 "Supply allowed values for PROPERTY."
873 (cond
874 ((and (equal property "BEAMER_env")
875 (not (org-entry-get nil (concat property "_ALL") 'inherit)))
876 ;; If no allowed values for BEAMER_env have been defined,
877 ;; supply all defined environments
878 (mapcar 'car (append org-e-beamer-environments-special
879 org-e-beamer-environments-extra
880 org-e-beamer-environments-default)))
881 ((and (equal property "BEAMER_col")
882 (not (org-entry-get nil (concat property "_ALL") 'inherit)))
883 ;; If no allowed values for BEAMER_col have been defined,
884 ;; supply some
885 (org-split-string org-e-beamer-column-widths " "))))
887 (add-hook 'org-property-allowed-value-functions
888 'org-e-beamer-allowed-property-values)
892 ;;; Commands
894 ;;;###autoload
895 (defun org-e-beamer-export-as-latex
896 (&optional subtreep visible-only body-only ext-plist)
897 "Export current buffer as a Beamer presentation.
899 If narrowing is active in the current buffer, only export its
900 narrowed part.
902 If a region is active, export that region.
904 When optional argument SUBTREEP is non-nil, export the sub-tree
905 at point, extracting information from the headline properties
906 first.
908 When optional argument VISIBLE-ONLY is non-nil, don't export
909 contents of hidden elements.
911 When optional argument BODY-ONLY is non-nil, only write code
912 between \"\\begin{document}\" and \"\\end{document}\".
914 EXT-PLIST, when provided, is a property list with external
915 parameters overriding Org default settings, but still inferior to
916 file-local settings.
918 Export is done in a buffer named \"*Org E-BEAMER Export*\". It
919 will be displayed if `org-export-show-temporary-export-buffer' is
920 non-nil."
921 (interactive)
922 (let ((outbuf (org-export-to-buffer
923 'e-beamer "*Org E-BEAMER Export*"
924 subtreep visible-only body-only ext-plist)))
925 (with-current-buffer outbuf (LaTeX-mode))
926 (when org-export-show-temporary-export-buffer
927 (switch-to-buffer-other-window outbuf))))
929 ;;;###autoload
930 (defun org-e-beamer-export-to-latex
931 (&optional subtreep visible-only body-only ext-plist pub-dir)
932 "Export current buffer as a Beamer presentation (tex).
934 If narrowing is active in the current buffer, only export its
935 narrowed part.
937 If a region is active, export that region.
939 When optional argument SUBTREEP is non-nil, export the sub-tree
940 at point, extracting information from the headline properties
941 first.
943 When optional argument VISIBLE-ONLY is non-nil, don't export
944 contents of hidden elements.
946 When optional argument BODY-ONLY is non-nil, only write code
947 between \"\\begin{document}\" and \"\\end{document}\".
949 EXT-PLIST, when provided, is a property list with external
950 parameters overriding Org default settings, but still inferior to
951 file-local settings.
953 When optional argument PUB-DIR is set, use it as the publishing
954 directory.
956 Return output file's name."
957 (interactive)
958 (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir)))
959 (org-export-to-file
960 'e-beamer outfile subtreep visible-only body-only ext-plist)))
962 ;;;###autoload
963 (defun org-e-beamer-export-to-pdf
964 (&optional subtreep visible-only body-only ext-plist pub-dir)
965 "Export current buffer as a BEAMER presentation (pdf).
967 If narrowing is active in the current buffer, only export its
968 narrowed part.
970 If a region is active, export that region.
972 When optional argument SUBTREEP is non-nil, export the sub-tree
973 at point, extracting information from the headline properties
974 first.
976 When optional argument VISIBLE-ONLY is non-nil, don't export
977 contents of hidden elements.
979 When optional argument BODY-ONLY is non-nil, only write code
980 between \"\\begin{document}\" and \"\\end{document}\".
982 EXT-PLIST, when provided, is a property list with external
983 parameters overriding Org default settings, but still inferior to
984 file-local settings.
986 When optional argument PUB-DIR is set, use it as the publishing
987 directory.
989 Return PDF file's name."
990 (interactive)
991 (org-e-latex-compile
992 (org-e-beamer-export-to-latex
993 subtreep visible-only body-only ext-plist pub-dir)))
995 ;;;###autoload
996 (defun org-e-beamer-select-environment ()
997 "Select the environment to be used by beamer for this entry.
998 While this uses (for convenience) a tag selection interface, the
999 result of this command will be that the BEAMER_env *property* of
1000 the entry is set.
1002 In addition to this, the command will also set a tag as a visual
1003 aid, but the tag does not have any semantic meaning."
1004 (interactive)
1005 ;; Make sure `org-e-beamer-environments-special' has a higher
1006 ;; priority than `org-e-beamer-environments-extra'.
1007 (let* ((envs (append org-e-beamer-environments-special
1008 org-e-beamer-environments-extra
1009 org-e-beamer-environments-default))
1010 (org-tag-alist
1011 (append '((:startgroup))
1012 (mapcar (lambda (e) (cons (concat "B_" (car e))
1013 (string-to-char (nth 1 e))))
1014 envs)
1015 '((:endgroup))
1016 '(("BMCOL" . ?|))))
1017 (org-fast-tag-selection-single-key t))
1018 (org-set-tags)
1019 (let ((tags (or (ignore-errors (org-get-tags-string)) "")))
1020 (cond
1021 ((equal org-last-tag-selection-key ?|)
1022 (if (string-match ":BMCOL:" tags)
1023 (org-set-property "BEAMER_col" (read-string "Column width: "))
1024 (org-delete-property "BEAMER_col")))
1025 ((string-match (concat ":B_\\("
1026 (mapconcat 'car envs "\\|")
1027 "\\):")
1028 tags)
1029 (org-entry-put nil "BEAMER_env" (match-string 1 tags)))
1030 (t (org-entry-delete nil "BEAMER_env"))))))
1032 ;;;###autoload
1033 (defun org-e-beamer-insert-options-template (&optional kind)
1034 "Insert a settings template, to make sure users do this right."
1035 (interactive (progn
1036 (message "Current [s]ubtree or [g]lobal?")
1037 (if (eq (read-char-exclusive) ?g) (list 'global)
1038 (list 'subtree))))
1039 (if (eq kind 'subtree)
1040 (progn
1041 (org-back-to-heading t)
1042 (org-reveal)
1043 (org-entry-put nil "LaTeX_CLASS" "beamer")
1044 (org-entry-put nil "LaTeX_CLASS_OPTIONS" "[presentation]")
1045 (org-entry-put nil "EXPORT_FILE_NAME" "presentation.pdf")
1046 (when org-e-beamer-column-view-format
1047 (org-entry-put nil "COLUMNS" org-e-beamer-column-view-format))
1048 (org-entry-put nil "BEAMER_col_ALL" org-e-beamer-column-widths))
1049 (insert "#+LaTeX_CLASS: beamer\n")
1050 (insert "#+LaTeX_CLASS_OPTIONS: [presentation]\n")
1051 (when org-e-beamer-theme
1052 (insert "#+BEAMER_THEME: " org-e-beamer-theme "\n"))
1053 (when org-e-beamer-column-view-format
1054 (insert "#+COLUMNS: " org-e-beamer-column-view-format "\n"))
1055 (insert "#+PROPERTY: BEAMER_col_ALL " org-e-beamer-column-widths "\n")))
1058 (provide 'org-e-beamer)
1059 ;;; org-e-beamer.el ends here