org-babel-exp-lob-one-liners should not parse the entire buffer.
[org-mode.git] / lisp / org-special-blocks.el
blob2da57f0338d800dfc6ffe2c8075fd383d3340692
2 ;; Copyright (C) 2009-2011 Free Software Foundation, Inc.
4 ;; Author: Chris Gray <chrismgray@gmail.com>
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software: you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (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.
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/>.
21 ;;; Commentary:
24 ;; This package generalizes the #+begin_foo and #+end_foo tokens.
26 ;; To use, put the following in your init file:
28 ;; (require 'org-special-blocks)
30 ;; The tokens #+begin_center, #+begin_verse, etc. existed previously.
31 ;; This package generalizes them (at least for the LaTeX and html
32 ;; exporters). When a #+begin_foo token is encountered by the LaTeX
33 ;; exporter, it is expanded into \begin{foo}. The text inside the
34 ;; environment is not protected, as text inside environments generally
35 ;; is. When #+begin_foo is encountered by the html exporter, a div
36 ;; with class foo is inserted into the HTML file. It is up to the
37 ;; user to add this class to his or her stylesheet if this div is to
38 ;; mean anything.
40 (require 'org-compat)
42 (defvar org-special-blocks-ignore-regexp "^\\(LaTeX\\|HTML\\)$"
43 "A regexp indicating the names of blocks that should be ignored
44 by org-special-blocks. These blocks will presumably be
45 interpreted by other mechanisms.")
47 (defvar org-export-current-backend) ; dynamically bound in org-exp.el
48 (defun org-special-blocks-make-special-cookies ()
49 "Adds special cookies when #+begin_foo and #+end_foo tokens are
50 seen. This is run after a few special cases are taken care of."
51 (when (or (eq org-export-current-backend 'html)
52 (eq org-export-current-backend 'latex))
53 (goto-char (point-min))
54 (while (re-search-forward "^[ \t]*#\\+\\(begin\\|end\\)_\\(.*\\)$" nil t)
55 (unless (org-string-match-p org-special-blocks-ignore-regexp (match-string 2))
56 (replace-match
57 (if (equal (downcase (match-string 1)) "begin")
58 (concat "ORG-" (match-string 2) "-START")
59 (concat "ORG-" (match-string 2) "-END"))
60 t t)))))
62 (add-hook 'org-export-preprocess-after-blockquote-hook
63 'org-special-blocks-make-special-cookies)
65 (defun org-special-blocks-convert-latex-special-cookies ()
66 "Converts the special cookies into LaTeX blocks."
67 (goto-char (point-min))
68 (while (re-search-forward "^ORG-\\([^ \t\n]*\\)[ \t]*\\(.*\\)-\\(START\\|END\\)$" nil t)
69 (replace-match
70 (if (equal (match-string 3) "START")
71 (concat "\\begin{" (match-string 1) "}" (match-string 2))
72 (concat "\\end{" (match-string 1) "}"))
73 t t)))
76 (add-hook 'org-export-latex-after-blockquotes-hook
77 'org-special-blocks-convert-latex-special-cookies)
79 (defvar line)
80 (defun org-special-blocks-convert-html-special-cookies ()
81 "Converts the special cookies into div blocks."
82 ;; Uses the dynamically-bound variable `line'.
83 (when (string-match "^ORG-\\(.*\\)-\\(START\\|END\\)$" line)
84 ; (org-close-par-maybe)
85 (message "%s" (match-string 1))
86 (if (equal (match-string 2 line) "START")
87 (insert "<div class=\"" (match-string 1 line) "\">\n")
88 (insert "</div>\n"))
89 (throw 'nextline nil)))
91 (add-hook 'org-export-html-after-blockquotes-hook
92 'org-special-blocks-convert-html-special-cookies)
94 (provide 'org-special-blocks)
96 ;;; org-special-blocks.el ends here