org-agenda.el: small indentation fix.
[org-mode.git] / lisp / org-special-blocks.el
blob590d30d97b25b84d05c71bc1d9ecbfcb132ebed1
1 ;; Copyright (C) 2009-2011 Free Software Foundation, Inc.
3 ;; Author: Chris Gray <chrismgray@gmail.com>
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software: you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; 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/>.
20 ;;; Commentary:
23 ;; This package generalizes the #+begin_foo and #+end_foo tokens.
25 ;; To use, put the following in your init file:
27 ;; (require 'org-special-blocks)
29 ;; The tokens #+begin_center, #+begin_verse, etc. existed previously.
30 ;; This package generalizes them (at least for the LaTeX and html
31 ;; exporters). When a #+begin_foo token is encountered by the LaTeX
32 ;; exporter, it is expanded into \begin{foo}. The text inside the
33 ;; environment is not protected, as text inside environments generally
34 ;; is. When #+begin_foo is encountered by the html exporter, a div
35 ;; with class foo is inserted into the HTML file. It is up to the
36 ;; user to add this class to his or her stylesheet if this div is to
37 ;; mean anything.
39 (require 'org-compat)
41 (defvar org-special-blocks-ignore-regexp "^\\(LaTeX\\|HTML\\)$"
42 "A regexp indicating the names of blocks that should be ignored
43 by org-special-blocks. These blocks will presumably be
44 interpreted by other mechanisms.")
46 (defvar org-export-current-backend) ; dynamically bound in org-exp.el
47 (defun org-special-blocks-make-special-cookies ()
48 "Adds special cookies when #+begin_foo and #+end_foo tokens are
49 seen. This is run after a few special cases are taken care of."
50 (when (or (eq org-export-current-backend 'html)
51 (eq org-export-current-backend 'latex))
52 (goto-char (point-min))
53 (while (re-search-forward "^[ \t]*#\\+\\(begin\\|end\\)_\\(.*\\)$" nil t)
54 (unless (org-string-match-p org-special-blocks-ignore-regexp (match-string 2))
55 (replace-match
56 (if (equal (downcase (match-string 1)) "begin")
57 (concat "ORG-" (match-string 2) "-START")
58 (concat "ORG-" (match-string 2) "-END"))
59 t t)))))
61 (add-hook 'org-export-preprocess-after-blockquote-hook
62 'org-special-blocks-make-special-cookies)
64 (defun org-special-blocks-convert-latex-special-cookies ()
65 "Converts the special cookies into LaTeX blocks."
66 (goto-char (point-min))
67 (while (re-search-forward "^ORG-\\([^ \t\n]*\\)[ \t]*\\(.*\\)-\\(START\\|END\\)$" nil t)
68 (replace-match
69 (if (equal (match-string 3) "START")
70 (concat "\\begin{" (match-string 1) "}" (match-string 2))
71 (concat "\\end{" (match-string 1) "}"))
72 t t)))
75 (add-hook 'org-export-latex-after-blockquotes-hook
76 'org-special-blocks-convert-latex-special-cookies)
78 (defvar line)
79 (defun org-special-blocks-convert-html-special-cookies ()
80 "Converts the special cookies into div blocks."
81 ;; Uses the dynamically-bound variable `line'.
82 (when (string-match "^ORG-\\(.*\\)-\\(START\\|END\\)$" line)
83 (message "%s" (match-string 1))
84 (when (equal (match-string 2 line) "START")
85 (org-close-par-maybe)
86 (insert "\n<div class=\"" (match-string 1 line) "\">")
87 (org-open-par))
88 (when (equal (match-string 2 line) "END")
89 (org-close-par-maybe)
90 (insert "\n</div>")
91 (org-open-par))
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