org-beamer.el 26 KB

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