doc: more explicit about the mechanism through which interactive evaluation of code...
[org-mode/org-jambu.git] / lisp / org-special-blocks.el
blobdbdcd408fb0c9ca119e77f235a9464345f187054
1 ;;; org-special-blocks.el --- Turn blocks into LaTeX envs and HTML divs
3 ;; Copyright (C) 2009 Chris Gray
5 ;; Author: Chris Gray <chrismgray@gmail.com>
7 ;; This file is not currently part of GNU Emacs.
9 ;; This program is free software; you can redistribute it and/or
10 ;; modify it under the terms of the GNU General Public License as
11 ;; published by the Free Software Foundation; either version 2, or (at
12 ;; your option) any later version.
14 ;; This program is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ;; General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program ; see the file COPYING. If not, write to
21 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
24 ;;; Commentary:
27 ;; This package generalizes the #+begin_foo and #+end_foo tokens.
29 ;; To use, put the following in your init file:
31 ;; (require 'org-special-blocks)
33 ;; The tokens #+begin_center, #+begin_verse, etc. existed previously.
34 ;; This package generalizes them (at least for the LaTeX and html
35 ;; exporters). When a #+begin_foo token is encountered by the LaTeX
36 ;; exporter, it is expanded into \begin{foo}. The text inside the
37 ;; environment is not protected, as text inside environments generally
38 ;; is. When #+begin_foo is encountered by the html exporter, a div
39 ;; with class foo is inserted into the HTML file. It is up to the
40 ;; user to add this class to his or her stylesheet if this div is to
41 ;; mean anything.
43 (require 'org-compat)
45 (defvar org-special-blocks-ignore-regexp "^\\(LaTeX\\|HTML\\)$"
46 "A regexp indicating the names of blocks that should be ignored
47 by org-special-blocks. These blocks will presumably be
48 interpreted by other mechanisms.")
50 (defvar org-export-current-backend) ; dynamically bound in org-exp.el
51 (defun org-special-blocks-make-special-cookies ()
52 "Adds special cookies when #+begin_foo and #+end_foo tokens are
53 seen. This is run after a few special cases are taken care of."
54 (when (or (eq org-export-current-backend 'html)
55 (eq org-export-current-backend 'latex))
56 (goto-char (point-min))
57 (while (re-search-forward "^[ \t]*#\\+\\(begin\\|end\\)_\\(.*\\)$" nil t)
58 (unless (org-string-match-p org-special-blocks-ignore-regexp (match-string 2))
59 (replace-match
60 (if (equal (downcase (match-string 1)) "begin")
61 (concat "ORG-" (match-string 2) "-START")
62 (concat "ORG-" (match-string 2) "-END"))
63 t t)))))
65 (add-hook 'org-export-preprocess-after-blockquote-hook
66 'org-special-blocks-make-special-cookies)
68 (defun org-special-blocks-convert-latex-special-cookies ()
69 "Converts the special cookies into LaTeX blocks."
70 (goto-char (point-min))
71 (while (re-search-forward "^ORG-\\([^ \t\n]*\\)[ \t]*\\(.*\\)-\\(START\\|END\\)$" nil t)
72 (replace-match
73 (if (equal (match-string 3) "START")
74 (concat "\\begin{" (match-string 1) "}" (match-string 2))
75 (concat "\\end{" (match-string 1) "}"))
76 t t)))
79 (add-hook 'org-export-latex-after-blockquotes-hook
80 'org-special-blocks-convert-latex-special-cookies)
82 (defvar line)
83 (defun org-special-blocks-convert-html-special-cookies ()
84 "Converts the special cookies into div blocks."
85 ;; Uses the dynamically-bound variable `line'.
86 (when (string-match "^ORG-\\(.*\\)-\\(START\\|END\\)$" line)
87 ; (org-close-par-maybe)
88 (message "%s" (match-string 1))
89 (if (equal (match-string 2 line) "START")
90 (insert "<div class=\"" (match-string 1 line) "\">\n")
91 (insert "</div>\n"))
92 (throw 'nextline nil)))
94 (add-hook 'org-export-html-after-blockquotes-hook
95 'org-special-blocks-convert-html-special-cookies)
97 (provide 'org-special-blocks)
99 ;;; org-special-blocks.el ends here