org-beamer.el 25 KB

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