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