Merge from emacs-24; up to 2012-12-26T22:30:58Z!yamaoka@jpl.org
[bpt/emacs.git] / lisp / org / org-special-blocks.el
CommitLineData
dfd98937 1;;; org-special-blocks.el --- handle Org special blocks
ab422c4d 2;; Copyright (C) 2009-2013 Free Software Foundation, Inc.
3ab2c837
BG
3
4;; Author: Chris Gray <chrismgray@gmail.com>
5
6;; This file is part of GNU Emacs.
7
09ade3a3
GM
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.
3ab2c837 12
09ade3a3
GM
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.
3ab2c837
BG
17
18;; You should have received a copy of the GNU General Public License
09ade3a3 19;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
3ab2c837
BG
20
21;;; Commentary:
22;;
23
24;; This package generalizes the #+begin_foo and #+end_foo tokens.
25
26;; To use, put the following in your init file:
27;;
28;; (require 'org-special-blocks)
29
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.
39
e66ba1df 40(require 'org-html)
3ab2c837
BG
41(require 'org-compat)
42
e66ba1df
BG
43(declare-function org-open-par "org-html" ())
44(declare-function org-close-par-maybe "org-html" ())
45
3ab2c837
BG
46(defvar org-special-blocks-ignore-regexp "^\\(LaTeX\\|HTML\\)$"
47 "A regexp indicating the names of blocks that should be ignored
48by org-special-blocks. These blocks will presumably be
49interpreted by other mechanisms.")
50
51(defvar org-export-current-backend) ; dynamically bound in org-exp.el
52(defun org-special-blocks-make-special-cookies ()
53 "Adds special cookies when #+begin_foo and #+end_foo tokens are
54seen. This is run after a few special cases are taken care of."
14e1337f 55 (when (or (eq org-export-current-backend 'html)
3ab2c837
BG
56 (eq org-export-current-backend 'latex))
57 (goto-char (point-min))
58 (while (re-search-forward "^[ \t]*#\\+\\(begin\\|end\\)_\\(.*\\)$" nil t)
59 (unless (org-string-match-p org-special-blocks-ignore-regexp (match-string 2))
60 (replace-match
61 (if (equal (downcase (match-string 1)) "begin")
62 (concat "ORG-" (match-string 2) "-START")
63 (concat "ORG-" (match-string 2) "-END"))
64 t t)))))
65
66(add-hook 'org-export-preprocess-after-blockquote-hook
67 'org-special-blocks-make-special-cookies)
68
69(defun org-special-blocks-convert-latex-special-cookies ()
70 "Converts the special cookies into LaTeX blocks."
71 (goto-char (point-min))
72 (while (re-search-forward "^ORG-\\([^ \t\n]*\\)[ \t]*\\(.*\\)-\\(START\\|END\\)$" nil t)
73 (replace-match
74 (if (equal (match-string 3) "START")
75 (concat "\\begin{" (match-string 1) "}" (match-string 2))
76 (concat "\\end{" (match-string 1) "}"))
77 t t)))
78
79
80(add-hook 'org-export-latex-after-blockquotes-hook
81 'org-special-blocks-convert-latex-special-cookies)
82
8223b1d2 83(defvar org-line)
3ab2c837
BG
84(defun org-special-blocks-convert-html-special-cookies ()
85 "Converts the special cookies into div blocks."
8223b1d2
BG
86 ;; Uses the dynamically-bound variable `org-line'.
87 (when (and org-line (string-match "^ORG-\\(.*\\)-\\(START\\|END\\)$" org-line))
3ab2c837 88 (message "%s" (match-string 1))
8223b1d2 89 (when (equal (match-string 2 org-line) "START")
e66ba1df 90 (org-close-par-maybe)
8223b1d2 91 (insert "\n<div class=\"" (match-string 1 org-line) "\">")
e66ba1df 92 (org-open-par))
8223b1d2 93 (when (equal (match-string 2 org-line) "END")
e66ba1df
BG
94 (org-close-par-maybe)
95 (insert "\n</div>")
96 (org-open-par))
3ab2c837
BG
97 (throw 'nextline nil)))
98
99(add-hook 'org-export-html-after-blockquotes-hook
100 'org-special-blocks-convert-html-special-cookies)
101
102(provide 'org-special-blocks)
103
104;;; org-special-blocks.el ends here