org-beamer.el 24 KB

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