org-beamer.el 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. ;;; org-beamer.el --- Beamer-specific LaTeX export for org-mode
  2. ;;
  3. ;; Copyright (C) 2007-2012 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten.dominik AT gmail DOT com>
  6. ;; Maintainer: Carsten Dominik <carsten.dominik AT gmail DOT com>
  7. ;; Keywords: org, wp, tex
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;;
  21. ;; This library implement the special treatment needed by using the
  22. ;; beamer class during LaTeX export.
  23. ;;; Code:
  24. (require 'org)
  25. (require 'org-exp)
  26. (defvar org-export-latex-header)
  27. (defvar org-export-latex-options-plist)
  28. (defvar org-export-opt-plist)
  29. (defgroup org-beamer nil
  30. "Options specific for using the beamer class in LaTeX export."
  31. :tag "Org Beamer"
  32. :group 'org-export-latex)
  33. (defcustom org-beamer-use-parts nil
  34. ""
  35. :group 'org-beamer
  36. :version "24.1"
  37. :type 'boolean)
  38. (defcustom org-beamer-frame-level 1
  39. "The level that should be interpreted as a frame.
  40. The levels above this one will be translated into a sectioning structure.
  41. Setting this to 2 will allow sections, 3 will allow subsections as well.
  42. You can set this to 4 as well, if you at the same time set
  43. `org-beamer-use-parts' to make the top levels `\part'."
  44. :group 'org-beamer
  45. :version "24.1"
  46. :type '(choice
  47. (const :tag "Frames need a BEAMER_env property" nil)
  48. (integer :tag "Specific level makes a frame")))
  49. (defcustom org-beamer-frame-default-options ""
  50. "Default options string to use for frames, should contains the [brackets].
  51. And example for this is \"[allowframebreaks]\"."
  52. :group 'org-beamer
  53. :version "24.1"
  54. :type '(string :tag "[options]"))
  55. (defcustom org-beamer-column-view-format
  56. "%45ITEM %10BEAMER_env(Env) %10BEAMER_envargs(Env Args) %4BEAMER_col(Col) %8BEAMER_extra(Extra)"
  57. "Default column view format that should be used to fill the template."
  58. :group 'org-beamer
  59. :version "24.1"
  60. :type '(choice
  61. (const :tag "Do not insert Beamer column view format" nil)
  62. (string :tag "Beamer column view format")))
  63. (defcustom org-beamer-themes
  64. "\\usetheme{default}\\usecolortheme{default}"
  65. "Default string to be used for extra heading stuff in beamer presentations.
  66. When a beamer template is filled, this will be the default for
  67. BEAMER_HEADER_EXTRA, which will be inserted just before \\begin{document}."
  68. :group 'org-beamer
  69. :version "24.1"
  70. :type '(choice
  71. (const :tag "Do not insert Beamer themes" nil)
  72. (string :tag "Beamer themes")))
  73. (defconst org-beamer-column-widths
  74. "0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 0.0 :ETC"
  75. "The column widths that should be installed as allowed property values.")
  76. (defconst org-beamer-transitions
  77. "\transblindsvertical \transblindshorizontal \transboxin \transboxout \transdissolve \transduration \transglitter \transsplithorizontalin \transsplithorizontalout \transsplitverticalin \transsplitverticalout \transwipe :ETC"
  78. "Transitions available for beamer.
  79. These are just a completion help.")
  80. (defconst org-beamer-environments-default
  81. '(("frame" "f" "dummy- special handling hard coded" "dummy")
  82. ("columns" "C" "\\begin{columns}%o %% %h%x" "\\end{columns}")
  83. ("column" "c" "\\begin{column}%o{%h\\textwidth}%x" "\\end{column}")
  84. ("block" "b" "\\begin{block}%a{%h}%x" "\\end{block}")
  85. ("alertblock" "a" "\\begin{alertblock}%a{%h}%x" "\\end{alertblock}")
  86. ("verse" "v" "\\begin{verse}%a %% %h%x" "\\end{verse}")
  87. ("quotation" "q" "\\begin{quotation}%a %% %h%x" "\\end{quotation}")
  88. ("quote" "Q" "\\begin{quote}%a %% %h%x" "\\end{quote}")
  89. ("structureenv" "s" "\\begin{structureenv}%a %% %h%x" "\\end{structureenv}")
  90. ("theorem" "t" "\\begin{theorem}%a%U%x" "\\end{theorem}")
  91. ("definition" "d" "\\begin{definition}%a%U%x" "\\end{definition}")
  92. ("example" "e" "\\begin{example}%a%U%x" "\\end{example}")
  93. ("exampleblock" "E" "\\begin{exampleblock}%a{%h}%x" "\\end{exampleblock}")
  94. ("proof" "p" "\\begin{proof}%a%U%x" "\\end{proof}")
  95. ("beamercolorbox" "o" "\\begin{beamercolorbox}%o{%h}%x" "\\end{beamercolorbox}")
  96. ("normal" "h" "%h" "") ; Emit the heading as normal text
  97. ("note" "n" "\\note%o%a{%h" "}")
  98. ("noteNH" "N" "\\note%o%a{" "}") ; note, ignore heading
  99. ("ignoreheading" "i" "%%%% %h" ""))
  100. "Environments triggered by properties in Beamer export.
  101. These are the defaults - for user definitions, see
  102. `org-beamer-environments-extra'.
  103. \"normal\" is a special fake environment, which emit the heading as
  104. normal text. It is needed when an environment should be surrounded
  105. by normal text. Since beamer export converts nodes into environments,
  106. you need to have a node to end the environment.
  107. For example
  108. ** a frame
  109. some text
  110. *** Blocktitle :B_block:
  111. inside the block
  112. *** After the block :B_normal:
  113. continuing here
  114. ** next frame")
  115. (defcustom org-beamer-environments-extra nil
  116. "Environments triggered by tags in Beamer export.
  117. Each entry has 4 elements:
  118. name Name of the environment
  119. key Selection key for `org-beamer-select-environment'
  120. open The opening template for the environment, with the following escapes
  121. %a the action/overlay specification
  122. %A the default action/overlay specification
  123. %o the options argument of the template
  124. %h the headline text
  125. %H if there is headline text, that text in {} braces
  126. %U if there is headline text, that text in [] brackets
  127. %x the content of the BEAMER_extra property
  128. close The closing string of the environment."
  129. :group 'org-beamer
  130. :version "24.1"
  131. :type '(repeat
  132. (list
  133. (string :tag "Environment")
  134. (string :tag "Selection key")
  135. (string :tag "Begin")
  136. (string :tag "End"))))
  137. (defvar org-beamer-frame-level-now nil)
  138. (defvar org-beamer-header-extra nil)
  139. (defvar org-beamer-export-is-beamer-p nil)
  140. (defvar org-beamer-inside-frame-at-level nil)
  141. (defvar org-beamer-columns-open nil)
  142. (defvar org-beamer-column-open nil)
  143. (defun org-beamer-cleanup-column-width (width)
  144. "Make sure the width is not empty, and that it has a unit."
  145. (setq width (org-trim (or width "")))
  146. (unless (string-match "\\S-" width) (setq width "0.5"))
  147. (if (string-match "\\`[.0-9]+\\'" width)
  148. (setq width (concat width "\\textwidth")))
  149. width)
  150. (defun org-beamer-open-column (&optional width opt)
  151. (org-beamer-close-column-maybe)
  152. (setq org-beamer-column-open t)
  153. (setq width (org-beamer-cleanup-column-width width))
  154. (insert (format "\\begin{column}%s{%s}\n" (or opt "") width)))
  155. (defun org-beamer-close-column-maybe ()
  156. (when org-beamer-column-open
  157. (setq org-beamer-column-open nil)
  158. (insert "\\end{column}\n")))
  159. (defun org-beamer-open-columns-maybe (&optional opts)
  160. (unless org-beamer-columns-open
  161. (setq org-beamer-columns-open t)
  162. (insert (format "\\begin{columns}%s\n" (or opts "")))))
  163. (defun org-beamer-close-columns-maybe ()
  164. (org-beamer-close-column-maybe)
  165. (when org-beamer-columns-open
  166. (setq org-beamer-columns-open nil)
  167. (insert "\\end{columns}\n")))
  168. (defun org-beamer-select-environment ()
  169. "Select the environment to be used by beamer for this entry.
  170. While this uses (for convenience) a tag selection interface, the result
  171. of this command will be that the BEAMER_env *property* of the entry is set.
  172. In addition to this, the command will also set a tag as a visual aid, but
  173. the tag does not have any semantic meaning."
  174. (interactive)
  175. (let* ((envs (append org-beamer-environments-extra
  176. org-beamer-environments-default))
  177. (org-tag-alist
  178. (append '((:startgroup))
  179. (mapcar (lambda (e) (cons (concat "B_" (car e))
  180. (string-to-char (nth 1 e))))
  181. envs)
  182. '((:endgroup))
  183. '(("BMCOL" . ?|))))
  184. (org-fast-tag-selection-single-key t))
  185. (org-set-tags)
  186. (let ((tags (or (ignore-errors (org-get-tags-string)) "")))
  187. (cond
  188. ((equal org-last-tag-selection-key ?|)
  189. (if (string-match ":BMCOL:" tags)
  190. (org-set-property "BEAMER_col" (read-string "Column width: "))
  191. (org-delete-property "BEAMER_col")))
  192. ((string-match (concat ":B_\\("
  193. (mapconcat 'car envs "\\|")
  194. "\\):")
  195. tags)
  196. (org-entry-put nil "BEAMER_env" (match-string 1 tags)))
  197. (t (org-entry-delete nil "BEAMER_env"))))))
  198. (defun org-beamer-sectioning (level text)
  199. "Return the sectioning entry for the current headline.
  200. LEVEL is the reduced level of the headline.
  201. TEXT is the text of the headline, everything except the leading stars.
  202. The return value is a cons cell. The car is the headline text, usually
  203. just TEXT, but possibly modified if options have been extracted from the
  204. text. The cdr is the sectioning entry, similar to what is given
  205. in org-export-latex-classes."
  206. (let* ((frame-level (or org-beamer-frame-level-now org-beamer-frame-level))
  207. (default
  208. (if org-beamer-use-parts
  209. '((1 . ("\\part{%s}" . "\\part*{%s}"))
  210. (2 . ("\\section{%s}" . "\\section*{%s}"))
  211. (3 . ("\\subsection{%s}" . "\\subsection*{%s}")))
  212. '((1 . ("\\section{%s}" . "\\section*{%s}"))
  213. (2 . ("\\subsection{%s}" . "\\subsection*{%s}")))))
  214. (envs (append org-beamer-environments-extra
  215. org-beamer-environments-default))
  216. (props (org-get-text-property-any 0 'org-props text))
  217. (in "") (out "") option action defaction environment extra
  218. columns-option column-option
  219. env have-text ass tmp)
  220. (if (= frame-level 0) (setq frame-level nil))
  221. (when (and org-beamer-inside-frame-at-level
  222. (<= level org-beamer-inside-frame-at-level))
  223. (setq org-beamer-inside-frame-at-level nil))
  224. (when (setq tmp (org-beamer-assoc-not-empty "BEAMER_col" props))
  225. (if (and (string-match "\\`[0-9.]+\\'" tmp)
  226. (or (= (string-to-number tmp) 1.0)
  227. (= (string-to-number tmp) 0.0)))
  228. ;; column width 1 means close columns, go back to full width
  229. (org-beamer-close-columns-maybe)
  230. (when (setq ass (assoc "BEAMER_envargs" props))
  231. (let (case-fold-search)
  232. (while (string-match "C\\(\\[[^][]*\\]\\|<[^<>]*>\\)" (cdr ass))
  233. (setq columns-option (match-string 1 (cdr ass)))
  234. (setcdr ass (replace-match "" t t (cdr ass))))
  235. (while (string-match "c\\(\\[[^][]*\\]\\|<[^<>]*>\\)" (cdr ass))
  236. (setq column-option (match-string 1 (cdr ass)))
  237. (setcdr ass (replace-match "" t t (cdr ass))))))
  238. (org-beamer-open-columns-maybe columns-option)
  239. (org-beamer-open-column tmp column-option)))
  240. (cond
  241. ((or (equal (cdr (assoc "BEAMER_env" props)) "frame")
  242. (and frame-level (= level frame-level)))
  243. ;; A frame
  244. (org-beamer-get-special props)
  245. (setq in (org-fill-template
  246. "\\begin{frame}%a%A%o%T%S%x"
  247. (list (cons "a" (or action ""))
  248. (cons "A" (or defaction ""))
  249. (cons "o" (or option org-beamer-frame-default-options ""))
  250. (cons "x" (if extra (concat "\n" extra) ""))
  251. (cons "h" "%s")
  252. (cons "T" (if (string-match "\\S-" text)
  253. "\n\\frametitle{%s}" ""))
  254. (cons "S" (if (string-match "\\\\\\\\" text)
  255. "\n\\framesubtitle{%s}" ""))))
  256. out (copy-sequence "\\end{frame}"))
  257. (org-add-props out
  258. '(org-insert-hook org-beamer-close-columns-maybe))
  259. (setq org-beamer-inside-frame-at-level level)
  260. (cons text (list in out in out)))
  261. ((and (setq env (cdr (assoc "BEAMER_env" props)))
  262. (setq ass (assoc env envs)))
  263. ;; A beamer environment selected by the BEAMER_env property
  264. (if (string-match "[ \t]+:[ \t]*$" text)
  265. (setq text (replace-match "" t t text)))
  266. (if (member env '("note" "noteNH"))
  267. ;; There should be no labels in a note, so we remove the targets
  268. ;; FIXME???
  269. (remove-text-properties 0 (length text) '(target nil) text))
  270. (org-beamer-get-special props)
  271. (setq text (org-trim text))
  272. (setq have-text (string-match "\\S-" text))
  273. (setq in (org-fill-template
  274. (nth 2 ass)
  275. (list (cons "a" (or action ""))
  276. (cons "A" (or defaction ""))
  277. (cons "o" (or option ""))
  278. (cons "x" (if extra (concat "\n" extra) ""))
  279. (cons "h" "%s")
  280. (cons "H" (if have-text (concat "{" text "}") ""))
  281. (cons "U" (if have-text (concat "[" text "]") ""))))
  282. out (nth 3 ass))
  283. (cond
  284. ((equal out "\\end{columns}")
  285. (setq org-beamer-columns-open t)
  286. (setq out (org-add-props (copy-sequence out)
  287. '(org-insert-hook
  288. (lambda ()
  289. (org-beamer-close-column-maybe)
  290. (setq org-beamer-columns-open nil))))))
  291. ((equal out "\\end{column}")
  292. (org-beamer-open-columns-maybe)))
  293. (cons text (list in out in out)))
  294. ((and (not org-beamer-inside-frame-at-level)
  295. (or (not frame-level)
  296. (< level frame-level))
  297. (assoc level default))
  298. ;; Normal sectioning
  299. (cons text (cdr (assoc level default))))
  300. (t nil))))
  301. (defvar extra)
  302. (defvar option)
  303. (defvar action)
  304. (defvar defaction)
  305. (defvar environment)
  306. (defun org-beamer-get-special (props)
  307. "Extract an option, action, and default action string from text.
  308. The variables option, action, defaction, extra are all scoped into
  309. this function dynamically."
  310. (let (tmp)
  311. (setq environment (org-beamer-assoc-not-empty "BEAMER_env" props))
  312. (setq extra (org-beamer-assoc-not-empty "BEAMER_extra" props))
  313. (when extra
  314. (setq extra (replace-regexp-in-string "\\\\n" "\n" extra)))
  315. (setq tmp (org-beamer-assoc-not-empty "BEAMER_envargs" props))
  316. (when tmp
  317. (setq tmp (copy-sequence tmp))
  318. (if (string-match "\\[<[^][<>]*>\\]" tmp)
  319. (setq defaction (match-string 0 tmp)
  320. tmp (replace-match "" t t tmp)))
  321. (if (string-match "\\[[^][]*\\]" tmp)
  322. (setq option (match-string 0 tmp)
  323. tmp (replace-match "" t t tmp)))
  324. (if (string-match "<[^<>]*>" tmp)
  325. (setq action (match-string 0 tmp)
  326. tmp (replace-match "" t t tmp))))))
  327. (defun org-beamer-assoc-not-empty (elt list)
  328. (let ((tmp (cdr (assoc elt list))))
  329. (and tmp (string-match "\\S-" tmp) tmp)))
  330. (defvar org-beamer-mode-map (make-sparse-keymap)
  331. "The keymap for `org-beamer-mode'.")
  332. (define-key org-beamer-mode-map "\C-c\C-b" 'org-beamer-select-environment)
  333. (define-minor-mode org-beamer-mode
  334. "Special support for editing Org-mode files made to export to beamer."
  335. nil " Bm" nil)
  336. (when (fboundp 'font-lock-add-keywords)
  337. (font-lock-add-keywords
  338. 'org-mode
  339. '((":\\(B_[a-z]+\\|BMCOL\\):" 1 'org-beamer-tag prepend))
  340. 'prepent))
  341. (defun org-beamer-place-default-actions-for-lists ()
  342. "Find default overlay specifications in items, and move them.
  343. The need to be after the begin statement of the environment."
  344. (when org-beamer-export-is-beamer-p
  345. (let (dovl)
  346. (goto-char (point-min))
  347. (while (re-search-forward
  348. "^[ \t]*\\\\begin{\\(itemize\\|enumerate\\|description\\)}[ \t\n]*\\\\item\\>\\( ?\\(<[^<>\n]*>\\|\\[[^][\n*]\\]\\)\\)?[ \t]*\\S-" nil t)
  349. (if (setq dovl (cdr (assoc "BEAMER_dovl"
  350. (get-text-property (match-end 0)
  351. 'org-props))))
  352. (save-excursion
  353. (goto-char (1+ (match-end 1)))
  354. (insert dovl)))))))
  355. (defun org-beamer-amend-header ()
  356. "Add `org-beamer-header-extra' to the LaTeX header.
  357. If the file contains the string BEAMER-HEADER-EXTRA-HERE on a line
  358. by itself, it will be replaced with `org-beamer-header-extra'. If not,
  359. the value will be inserted right after the documentclass statement."
  360. (when (and org-beamer-export-is-beamer-p
  361. org-beamer-header-extra)
  362. (goto-char (point-min))
  363. (cond
  364. ((re-search-forward
  365. "^[ \t]*\\[?BEAMER-HEADER-EXTRA\\(-HERE\\)?\\]?[ \t]*$" nil t)
  366. (replace-match org-beamer-header-extra t t)
  367. (or (bolp) (insert "\n")))
  368. ((re-search-forward "^[ \t]*\\\\begin{document}" nil t)
  369. (beginning-of-line 1)
  370. (insert org-beamer-header-extra)
  371. (or (bolp) (insert "\n"))))))
  372. (defcustom org-beamer-fragile-re "\\\\\\(verb\\|lstinline\\)\\|^[ \t]*\\\\begin{\\(verbatim\\|lstlisting\\|minted\\)}"
  373. "If this regexp matches in a frame, the frame is marked as fragile."
  374. :group 'org-beamer
  375. :version "24.1"
  376. :type 'regexp)
  377. (defface org-beamer-tag '((t (:box (:line-width 1 :color grey40))))
  378. "The special face for beamer tags."
  379. :group 'org-beamer)
  380. ;; Functions to initialize and post-process
  381. ;; These functions will be hooked into various places in the export process
  382. (defun org-beamer-initialize-open-trackers ()
  383. "Reset variables that track if certain environments are open during export."
  384. (setq org-beamer-columns-open nil)
  385. (setq org-beamer-column-open nil)
  386. (setq org-beamer-inside-frame-at-level nil)
  387. (setq org-beamer-export-is-beamer-p nil))
  388. (defun org-beamer-after-initial-vars ()
  389. "Find special settings for beamer and store them.
  390. The effect is that these values will be accessible during export."
  391. ;; First verify that we are exporting using the beamer class
  392. (setq org-beamer-export-is-beamer-p
  393. (string-match "\\\\documentclass\\(\\[[^][]*?\\]\\)?{beamer}"
  394. org-export-latex-header))
  395. (when org-beamer-export-is-beamer-p
  396. ;; Find the frame level
  397. (setq org-beamer-frame-level-now
  398. (or (and (org-region-active-p)
  399. (save-excursion
  400. (goto-char (region-beginning))
  401. (and (looking-at org-complex-heading-regexp)
  402. (org-entry-get nil "BEAMER_FRAME_LEVEL" 'selective))))
  403. (save-excursion
  404. (save-restriction
  405. (widen)
  406. (goto-char (point-min))
  407. (and (re-search-forward
  408. "^#\\+BEAMER_FRAME_LEVEL:[ \t]*\\(.*?\\)[ \t]*$" nil t)
  409. (match-string 1))))
  410. (plist-get org-export-latex-options-plist :beamer-frame-level)
  411. org-beamer-frame-level))
  412. ;; Normalize the value so that the functions can trust the value
  413. (cond
  414. ((not org-beamer-frame-level-now)
  415. (setq org-beamer-frame-level-now nil))
  416. ((stringp org-beamer-frame-level-now)
  417. (setq org-beamer-frame-level-now
  418. (string-to-number org-beamer-frame-level-now))))
  419. ;; Find the header additions, most likely theme commands
  420. (setq org-beamer-header-extra
  421. (or (and (org-region-active-p)
  422. (save-excursion
  423. (goto-char (region-beginning))
  424. (and (looking-at org-complex-heading-regexp)
  425. (org-entry-get nil "BEAMER_HEADER_EXTRA"
  426. 'selective))))
  427. (save-excursion
  428. (save-restriction
  429. (widen)
  430. (let ((txt ""))
  431. (goto-char (point-min))
  432. (while (re-search-forward
  433. "^#\\+BEAMER_HEADER_EXTRA:[ \t]*\\(.*?\\)[ \t]*$"
  434. nil t)
  435. (setq txt (concat txt "\n" (match-string 1))))
  436. (if (> (length txt) 0) (substring txt 1)))))
  437. (plist-get org-export-latex-options-plist
  438. :beamer-header-extra)))
  439. (let ((inhibit-read-only t)
  440. (case-fold-search nil)
  441. props)
  442. (org-unmodified
  443. (remove-text-properties (point-min) (point-max) '(org-props nil))
  444. (org-map-entries
  445. '(progn
  446. (setq props (org-entry-properties nil 'standard))
  447. (if (and (not (assoc "BEAMER_env" props))
  448. (looking-at ".*?:B_\\(note\\(NH\\)?\\):"))
  449. (push (cons "BEAMER_env" (match-string 1)) props))
  450. (put-text-property (point-at-bol) (point-at-eol) 'org-props props)))
  451. (setq org-export-latex-options-plist
  452. (plist-put org-export-latex-options-plist :tags nil))))))
  453. (defun org-beamer-auto-fragile-frames ()
  454. "Mark any frames containing verbatim environments as fragile.
  455. This function will run in the final LaTeX document."
  456. (when org-beamer-export-is-beamer-p
  457. (let (opts)
  458. (goto-char (point-min))
  459. ;; Find something that might be fragile
  460. (while (re-search-forward org-beamer-fragile-re nil t)
  461. (save-excursion
  462. ;; Are we inside a frame here?
  463. (when (and (re-search-backward "^[ \t]*\\\\\\(begin\\|end\\){frame}"
  464. nil t)
  465. (equal (match-string 1) "begin"))
  466. ;; yes, inside a frame, make sure "fragile" is one of the options
  467. (goto-char (match-end 0))
  468. (if (not (looking-at "\\[.*?\\]"))
  469. (insert "[fragile]")
  470. (setq opts (substring (match-string 0) 1 -1))
  471. (delete-region (match-beginning 0) (match-end 0))
  472. (setq opts (org-split-string opts ","))
  473. (add-to-list 'opts "fragile")
  474. (insert "[" (mapconcat 'identity opts ",") "]"))))))))
  475. (defcustom org-beamer-outline-frame-title "Outline"
  476. "Default title of a frame containing an outline."
  477. :group 'org-beamer
  478. :version "24.1"
  479. :type '(string :tag "Outline frame title")
  480. )
  481. (defcustom org-beamer-outline-frame-options nil
  482. "Outline frame options appended after \\begin{frame}.
  483. You might want to put e.g. [allowframebreaks=0.9] here. Remember to
  484. include square brackets."
  485. :group 'org-beamer
  486. :version "24.1"
  487. :type '(string :tag "Outline frame options")
  488. )
  489. (defun org-beamer-fix-toc ()
  490. "Fix the table of contents by removing the vspace line."
  491. (when org-beamer-export-is-beamer-p
  492. (save-excursion
  493. (goto-char (point-min))
  494. (when (re-search-forward "\\(\\\\setcounter{tocdepth.*\n\\\\tableofcontents.*\n\\)\\(\\\\vspace\\*.*\\)"
  495. nil t)
  496. (replace-match
  497. (concat "\\\\begin{frame}" org-beamer-outline-frame-options
  498. "\n\\\\frametitle{"
  499. org-beamer-outline-frame-title
  500. "}\n\\1\\\\end{frame}")
  501. t nil)))))
  502. (defun org-beamer-property-changed (property value)
  503. "Track the BEAMER_env property with tags."
  504. (cond
  505. ((equal property "BEAMER_env")
  506. (save-excursion
  507. (org-back-to-heading t)
  508. (let ((tags (org-get-tags)))
  509. (setq tags (delq nil (mapcar (lambda (x)
  510. (if (string-match "^B_" x) nil x))
  511. tags)))
  512. (org-set-tags-to tags))
  513. (when (and value (stringp value) (string-match "\\S-" value))
  514. (org-toggle-tag (concat "B_" value) 'on))))
  515. ((equal property "BEAMER_col")
  516. (org-toggle-tag "BMCOL" (if (and value (string-match "\\S-" value))
  517. 'on 'off)))))
  518. (defun org-beamer-select-beamer-code ()
  519. "Take code marked for BEAMER and turn it into marked for LaTeX."
  520. (when org-beamer-export-is-beamer-p
  521. (goto-char (point-min))
  522. (while (re-search-forward
  523. "^\\([ \]*#\\+\\(begin_\\|end_\\)?\\)\\(beamer\\)\\>" nil t)
  524. (replace-match "\\1latex"))))
  525. ;; OK, hook all these functions into appropriate places
  526. (add-hook 'org-export-first-hook
  527. 'org-beamer-initialize-open-trackers)
  528. (add-hook 'org-property-changed-functions
  529. 'org-beamer-property-changed)
  530. (add-hook 'org-export-latex-after-initial-vars-hook
  531. 'org-beamer-after-initial-vars)
  532. (add-hook 'org-export-latex-final-hook
  533. 'org-beamer-place-default-actions-for-lists)
  534. (add-hook 'org-export-latex-final-hook
  535. 'org-beamer-auto-fragile-frames)
  536. (add-hook 'org-export-latex-final-hook
  537. 'org-beamer-fix-toc)
  538. (add-hook 'org-export-latex-final-hook
  539. 'org-beamer-amend-header)
  540. (add-hook 'org-export-preprocess-before-selecting-backend-code-hook
  541. 'org-beamer-select-beamer-code)
  542. (defun org-insert-beamer-options-template (kind)
  543. "Insert a settings template, to make sure users do this right."
  544. (interactive (progn
  545. (message "Current [s]ubtree or [g]lobal?")
  546. (if (equal (read-char-exclusive) ?g)
  547. (list 'global)
  548. (list 'subtree))))
  549. (if (eq kind 'subtree)
  550. (progn
  551. (org-back-to-heading t)
  552. (org-reveal)
  553. (org-entry-put nil "LaTeX_CLASS" "beamer")
  554. (org-entry-put nil "LaTeX_CLASS_OPTIONS" "[presentation]")
  555. (org-entry-put nil "EXPORT_FILE_NAME" "presentation.pdf")
  556. (org-entry-put nil "BEAMER_FRAME_LEVEL" (number-to-string
  557. org-beamer-frame-level))
  558. (when org-beamer-themes
  559. (org-entry-put nil "BEAMER_HEADER_EXTRA" org-beamer-themes))
  560. (when org-beamer-column-view-format
  561. (org-entry-put nil "COLUMNS" org-beamer-column-view-format))
  562. (org-entry-put nil "BEAMER_col_ALL" "0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 :ETC"))
  563. (insert "#+LaTeX_CLASS: beamer\n")
  564. (insert "#+LaTeX_CLASS_OPTIONS: [presentation]\n")
  565. (insert (format "#+BEAMER_FRAME_LEVEL: %d\n" org-beamer-frame-level) "\n")
  566. (when org-beamer-themes
  567. (insert "#+BEAMER_HEADER_EXTRA: " org-beamer-themes "\n"))
  568. (when org-beamer-column-view-format
  569. (insert "#+COLUMNS: " org-beamer-column-view-format "\n"))
  570. (insert "#+PROPERTY: BEAMER_col_ALL 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 :ETC\n")))
  571. (defun org-beamer-allowed-property-values (property)
  572. "Supply allowed values for BEAMER properties."
  573. (cond
  574. ((and (equal property "BEAMER_env")
  575. (not (org-entry-get nil (concat property "_ALL") 'inherit)))
  576. ;; If no allowed values for BEAMER_env have been defined,
  577. ;; supply all defined environments
  578. (mapcar 'car (append org-beamer-environments-extra
  579. org-beamer-environments-default)))
  580. ((and (equal property "BEAMER_col")
  581. (not (org-entry-get nil (concat property "_ALL") 'inherit)))
  582. ;; If no allowed values for BEAMER_col have been defined,
  583. ;; supply some
  584. '("0.1" "0.2" "0.3" "0.4" "0.5" "0.6" "0.7" "0.8" "0.9" "" ":ETC"))
  585. (t nil)))
  586. (add-hook 'org-property-allowed-value-functions
  587. 'org-beamer-allowed-property-values)
  588. (provide 'org-beamer)
  589. ;;; org-beamer.el ends here