org-e-beamer.el 39 KB

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