org-e-beamer.el 38 KB

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