org-e-beamer.el 39 KB

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