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