org-beamer.el 25 KB

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