org-beamer.el 25 KB

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