org-e-beamer.el 38 KB

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