org-beamer.el 25 KB

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