org-e-beamer.el 39 KB

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