org-e-beamer.el 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  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 (not class-options) header
  667. (replace-regexp-in-string
  668. "^[ \t]*\\\\documentclass\\(\\(\\[.*\\]\\)?\\)"
  669. class-options header t nil 1)))))
  670. (when document-class-string
  671. (org-e-latex--guess-babel-language
  672. (org-e-latex--guess-inputenc
  673. (org-splice-latex-header
  674. document-class-string
  675. org-export-latex-default-packages-alist ; defined in org.el
  676. org-export-latex-packages-alist nil ; defined in org.el
  677. (plist-get info :latex-header-extra)))
  678. info)))))
  679. ;; 3. Insert themes.
  680. (let ((format-theme
  681. (function
  682. (lambda (prop command)
  683. (let ((theme (plist-get info prop)))
  684. (when theme
  685. (concat command
  686. (if (not (string-match "\\[.*\\]" theme))
  687. (format "{%s}\n" theme)
  688. (format "%s{%s}\n"
  689. (match-string 0 theme)
  690. (org-trim
  691. (replace-match "" nil nil theme)))))))))))
  692. (mapconcat (lambda (args) (apply format-theme args))
  693. '((:beamer-theme "\\usetheme")
  694. (:beamer-color-theme "\\usecolortheme")
  695. (:beamer-font-theme "\\usefonttheme")
  696. (:beamer-inner-theme "\\useinnertheme")
  697. (:beamer-outer-theme "\\useoutertheme"))
  698. ""))
  699. ;; 4. Possibly limit depth for headline numbering.
  700. (let ((sec-num (plist-get info :section-numbers)))
  701. (when (integerp sec-num)
  702. (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
  703. ;; 5. Author.
  704. (let ((author (and (plist-get info :with-author)
  705. (let ((auth (plist-get info :author)))
  706. (and auth (org-export-data auth info)))))
  707. (email (and (plist-get info :with-email)
  708. (org-export-data (plist-get info :email) info))))
  709. (cond ((and author email (not (string= "" email)))
  710. (format "\\author{%s\\thanks{%s}}\n" author email))
  711. (author (format "\\author{%s}\n" author))
  712. (t "\\author{}\n")))
  713. ;; 6. Date.
  714. (format "\\date{%s}\n" (org-export-data (plist-get info :date) info))
  715. ;; 7. Title
  716. (format "\\title{%s}\n" title)
  717. ;; 8. Hyperref options.
  718. (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
  719. (or (plist-get info :keywords) "")
  720. (or (plist-get info :description) "")
  721. (if (not (plist-get info :with-creator)) ""
  722. (plist-get info :creator)))
  723. ;; 9. Document start.
  724. "\\begin{document}\n\n"
  725. ;; 10. Title command.
  726. (org-element-normalize-string
  727. (cond ((string= "" title) nil)
  728. ((not (stringp org-e-latex-title-command)) nil)
  729. ((string-match "\\(?:[^%]\\|^\\)%s"
  730. org-e-latex-title-command)
  731. (format org-e-latex-title-command title))
  732. (t org-e-latex-title-command)))
  733. ;; 11. Table of contents.
  734. (let ((depth (plist-get info :with-toc)))
  735. (when depth
  736. (concat
  737. (format "\\begin{frame}%s{%s}\n"
  738. (org-e-beamer--normalize-argument
  739. org-e-beamer-outline-frame-options 'option)
  740. org-e-beamer-outline-frame-title)
  741. (when (wholenump depth)
  742. (format "\\setcounter{tocdepth}{%d}\n" depth))
  743. "\\tableofcontents\n"
  744. "\\end{frame}\n\n")))
  745. ;; 12. Document's body.
  746. contents
  747. ;; 13. Creator.
  748. (let ((creator-info (plist-get info :with-creator)))
  749. (cond
  750. ((not creator-info) "")
  751. ((eq creator-info 'comment)
  752. (format "%% %s\n" (plist-get info :creator)))
  753. (t (concat (plist-get info :creator) "\n"))))
  754. ;; 14. Document end.
  755. "\\end{document}")))
  756. ;;; Minor Mode
  757. (defvar org-e-beamer-mode-map (make-sparse-keymap)
  758. "The keymap for `org-e-beamer-mode'.")
  759. (define-key org-e-beamer-mode-map "\C-c\C-b" 'org-e-beamer-select-environment)
  760. ;;;###autoload
  761. (define-minor-mode org-e-beamer-mode
  762. "Support for editing Beamer oriented Org mode files."
  763. nil " Bm" 'org-e-beamer-mode-map)
  764. (when (fboundp 'font-lock-add-keywords)
  765. (font-lock-add-keywords
  766. 'org-mode
  767. '((":\\(B_[a-z]+\\|BMCOL\\):" 1 'org-e-beamer-tag prepend))
  768. 'prepend))
  769. (defface org-e-beamer-tag '((t (:box (:line-width 1 :color grey40))))
  770. "The special face for beamer tags."
  771. :group 'org-export-e-beamer)
  772. (defun org-e-beamer-property-changed (property value)
  773. "Track the BEAMER_env property with tags.
  774. PROPERTY is the name of the modified property. VALUE is its new
  775. value."
  776. (cond
  777. ((equal property "BEAMER_env")
  778. (save-excursion
  779. (org-back-to-heading t)
  780. (let ((tags (org-get-tags)))
  781. (setq tags (delq nil (mapcar (lambda (x)
  782. (if (string-match "^B_" x) nil x))
  783. tags)))
  784. (org-set-tags-to tags))
  785. (when (org-string-nw-p value) (org-toggle-tag (concat "B_" value) 'on))))
  786. ((equal property "BEAMER_col")
  787. (org-toggle-tag "BMCOL" (if (org-string-nw-p value) 'on 'off)))))
  788. (add-hook 'org-property-changed-functions 'org-e-beamer-property-changed)
  789. (defun org-e-beamer-allowed-property-values (property)
  790. "Supply allowed values for PROPERTY."
  791. (cond
  792. ((and (equal property "BEAMER_env")
  793. (not (org-entry-get nil (concat property "_ALL") 'inherit)))
  794. ;; If no allowed values for BEAMER_env have been defined,
  795. ;; supply all defined environments
  796. (mapcar 'car (append org-e-beamer-environments-special
  797. org-e-beamer-environments-extra
  798. org-e-beamer-environments-default)))
  799. ((and (equal property "BEAMER_col")
  800. (not (org-entry-get nil (concat property "_ALL") 'inherit)))
  801. ;; If no allowed values for BEAMER_col have been defined,
  802. ;; supply some
  803. (org-split-string org-e-beamer-column-widths " "))))
  804. (add-hook 'org-property-allowed-value-functions
  805. 'org-e-beamer-allowed-property-values)
  806. ;;; Commands
  807. ;;;###autoload
  808. (defun org-e-beamer-export-as-latex
  809. (&optional subtreep visible-only body-only ext-plist)
  810. "Export current buffer as a Beamer buffer.
  811. If narrowing is active in the current buffer, only export its
  812. narrowed part.
  813. If a region is active, export that region.
  814. When optional argument SUBTREEP is non-nil, export the sub-tree
  815. at point, extracting information from the headline properties
  816. first.
  817. When optional argument VISIBLE-ONLY is non-nil, don't export
  818. contents of hidden elements.
  819. When optional argument BODY-ONLY is non-nil, only write code
  820. between \"\\begin{document}\" and \"\\end{document}\".
  821. EXT-PLIST, when provided, is a property list with external
  822. parameters overriding Org default settings, but still inferior to
  823. file-local settings.
  824. Export is done in a buffer named \"*Org E-BEAMER Export*\", which
  825. will be displayed when `org-export-show-temporary-export-buffer'
  826. is non-nil."
  827. (interactive)
  828. (let ((outbuf (org-export-to-buffer
  829. 'e-beamer "*Org E-BEAMER Export*"
  830. subtreep visible-only body-only ext-plist)))
  831. (with-current-buffer outbuf (LaTeX-mode))
  832. (when org-export-show-temporary-export-buffer
  833. (switch-to-buffer-other-window outbuf))))
  834. ;;;###autoload
  835. (defun org-e-beamer-export-to-latex
  836. (&optional subtreep visible-only body-only ext-plist pub-dir)
  837. "Export current buffer as a Beamer presentation (tex).
  838. If narrowing is active in the current buffer, only export its
  839. narrowed part.
  840. If a region is active, export that region.
  841. When optional argument SUBTREEP is non-nil, export the sub-tree
  842. at point, extracting information from the headline properties
  843. first.
  844. When optional argument VISIBLE-ONLY is non-nil, don't export
  845. contents of hidden elements.
  846. When optional argument BODY-ONLY is non-nil, only write code
  847. between \"\\begin{document}\" and \"\\end{document}\".
  848. EXT-PLIST, when provided, is a property list with external
  849. parameters overriding Org default settings, but still inferior to
  850. file-local settings.
  851. When optional argument PUB-DIR is set, use it as the publishing
  852. directory.
  853. Return output file's name."
  854. (interactive)
  855. (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir)))
  856. (org-export-to-file
  857. 'e-beamer outfile subtreep visible-only body-only ext-plist)))
  858. ;;;###autoload
  859. (defun org-e-beamer-export-to-pdf
  860. (&optional subtreep visible-only body-only ext-plist pub-dir)
  861. "Export current buffer as a Beamer presentation (PDF).
  862. If narrowing is active in the current buffer, only export its
  863. narrowed part.
  864. If a region is active, export that region.
  865. When optional argument SUBTREEP is non-nil, export the sub-tree
  866. at point, extracting information from the headline properties
  867. first.
  868. When optional argument VISIBLE-ONLY is non-nil, don't export
  869. contents of hidden elements.
  870. When optional argument BODY-ONLY is non-nil, only write code
  871. between \"\\begin{document}\" and \"\\end{document}\".
  872. EXT-PLIST, when provided, is a property list with external
  873. parameters overriding Org default settings, but still inferior to
  874. file-local settings.
  875. When optional argument PUB-DIR is set, use it as the publishing
  876. directory.
  877. Return PDF file's name."
  878. (interactive)
  879. (org-e-latex-compile
  880. (org-e-beamer-export-to-latex
  881. subtreep visible-only body-only ext-plist pub-dir)))
  882. ;;;###autoload
  883. (defun org-e-beamer-select-environment ()
  884. "Select the environment to be used by beamer for this entry.
  885. While this uses (for convenience) a tag selection interface, the
  886. result of this command will be that the BEAMER_env *property* of
  887. the entry is set.
  888. In addition to this, the command will also set a tag as a visual
  889. aid, but the tag does not have any semantic meaning."
  890. (interactive)
  891. ;; Make sure `org-e-beamer-environments-special' has a higher
  892. ;; priority than `org-e-beamer-environments-extra'.
  893. (let* ((envs (append org-e-beamer-environments-special
  894. org-e-beamer-environments-extra
  895. org-e-beamer-environments-default))
  896. (org-tag-alist
  897. (append '((:startgroup))
  898. (mapcar (lambda (e) (cons (concat "B_" (car e))
  899. (string-to-char (nth 1 e))))
  900. envs)
  901. '((:endgroup))
  902. '(("BMCOL" . ?|))))
  903. (org-fast-tag-selection-single-key t))
  904. (org-set-tags)
  905. (let ((tags (or (ignore-errors (org-get-tags-string)) "")))
  906. (cond
  907. ((eq org-last-tag-selection-key ?|)
  908. (if (string-match ":BMCOL:" tags)
  909. (org-set-property "BEAMER_col" (read-string "Column width: "))
  910. (org-delete-property "BEAMER_col")))
  911. ((string-match (concat ":B_\\("
  912. (mapconcat 'car envs "\\|")
  913. "\\):")
  914. tags)
  915. (org-entry-put nil "BEAMER_env" (match-string 1 tags)))
  916. (t (org-entry-delete nil "BEAMER_env"))))))
  917. ;;;###autoload
  918. (defun org-e-beamer-insert-options-template (&optional kind)
  919. "Insert a settings template, to make sure users do this right."
  920. (interactive (progn
  921. (message "Current [s]ubtree or [g]lobal?")
  922. (if (eq (read-char-exclusive) ?g) (list 'global)
  923. (list 'subtree))))
  924. (if (eq kind 'subtree)
  925. (progn
  926. (org-back-to-heading t)
  927. (org-reveal)
  928. (org-entry-put nil "LaTeX_CLASS" "beamer")
  929. (org-entry-put nil "LaTeX_CLASS_OPTIONS" "[presentation]")
  930. (org-entry-put nil "EXPORT_FILE_NAME" "presentation.pdf")
  931. (when org-e-beamer-column-view-format
  932. (org-entry-put nil "COLUMNS" org-e-beamer-column-view-format))
  933. (org-entry-put nil "BEAMER_col_ALL" org-e-beamer-column-widths))
  934. (insert "#+LaTeX_CLASS: beamer\n")
  935. (insert "#+LaTeX_CLASS_OPTIONS: [presentation]\n")
  936. (when org-e-beamer-theme
  937. (insert "#+BEAMER_THEME: " org-e-beamer-theme "\n"))
  938. (when org-e-beamer-column-view-format
  939. (insert "#+COLUMNS: " org-e-beamer-column-view-format "\n"))
  940. (insert "#+PROPERTY: BEAMER_col_ALL " org-e-beamer-column-widths "\n")))
  941. (provide 'org-e-beamer)
  942. ;;; org-e-beamer.el ends here