Fix bug in iswitchb completion when the table is already a list
[org-mode/org-tableheadings.git] / lisp / org-exp-blocks.el
blob646688d4a466f8c70df7d229f404ac1f44a95d90
1 ;;; org-exp-blocks.el --- pre-process blocks when exporting org files
3 ;; Copyright (C) 2009
4 ;; Free Software Foundation, Inc.
6 ;; Author: Eric Schulte
8 ;; This file is part of GNU Emacs.
9 ;;
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; This is a utility for pre-processing blocks in org files before
26 ;; export using the `org-export-preprocess-hook'. It can be used for
27 ;; exporting new types of blocks from org-mode files and also for
28 ;; changing the default export behavior of existing org-mode blocks.
29 ;; The `org-export-blocks' and `org-export-interblocks' variables can
30 ;; be used to control how blocks and the spaces between blocks
31 ;; respectively are processed upon export.
33 ;; The type of a block is defined as the string following =#+begin_=,
34 ;; so for example the following block would be of type ditaa. Note
35 ;; that both upper or lower case are allowed in =#+BEGIN_= and
36 ;; =#+END_=.
38 ;; #+begin_ditaa blue.png -r -S
39 ;; +---------+
40 ;; | cBLU |
41 ;; | |
42 ;; | +----+
43 ;; | |cPNK|
44 ;; | | |
45 ;; +----+----+
46 ;; #+end_ditaa
48 ;;; Currently Implemented Block Types
50 ;; ditaa :: Convert ascii pictures to actual images using ditaa
51 ;; http://ditaa.sourceforge.net/. To use this set
52 ;; `org-ditaa-jar-path' to the path to ditaa.jar on your
53 ;; system (should be set automatically in most cases) .
55 ;; dot :: Convert graphs defined using the dot graphing language to
56 ;; images using the dot utility. For information on dot see
57 ;; http://www.graphviz.org/
59 ;; comment :: Wrap comments with titles and author information, in
60 ;; their own divs with author-specific ids allowing for css
61 ;; coloring of comments based on the author.
63 ;;; Adding new blocks
65 ;; When adding a new block type first define a formatting function
66 ;; along the same lines as `org-export-blocks-format-dot' and then use
67 ;; `org-export-blocks-add-block' to add your block type to
68 ;; `org-export-blocks'.
70 (eval-when-compile
71 (require 'cl))
72 (require 'org)
74 (defvar htmlp)
75 (defvar latexp)
76 (defvar docbookp)
77 (defvar asciip)
79 (defun org-export-blocks-set (var value)
80 "Set the value of `org-export-blocks' and install fontification."
81 (set var value)
82 (mapc (lambda (spec)
83 (if (nth 2 spec)
84 (setq org-protecting-blocks
85 (delete (symbol-name (car spec))
86 org-protecting-blocks))
87 (add-to-list 'org-protecting-blocks
88 (symbol-name (car spec)))))
89 value))
91 (defcustom org-export-blocks
92 '((comment org-export-blocks-format-comment t)
93 (ditaa org-export-blocks-format-ditaa nil)
94 (dot org-export-blocks-format-dot nil))
95 "Use this a-list to associate block types with block exporting
96 functions. The type of a block is determined by the text
97 immediately following the '#+BEGIN_' portion of the block header.
98 Each block export function should accept three argumets..."
99 :group 'org-export-general
100 :type '(repeat
101 (list
102 (symbol :tag "Block name")
103 (function :tag "Block formatter")
104 (boolean :tag "Fontify content as Org syntax")))
105 :set 'org-export-blocks-set)
107 (defun org-export-blocks-add-block (block-spec)
108 "Add a new block type to `org-export-blocks'. BLOCK-SPEC
109 should be a three element list the first element of which should
110 indicate the name of the block, the second element should be the
111 formatting function called by `org-export-blocks-preprocess' and
112 the third element a flag indicating whether these types of blocks
113 should be fontified in org-mode buffers (see
114 `org-protecting-blocks'). For example the BLOCK-SPEC for ditaa
115 blocks is as follows...
117 (ditaa org-export-blocks-format-ditaa nil)"
118 (unless (member block-spec org-export-blocks)
119 (setq org-export-blocks (cons block-spec org-export-blocks))
120 (org-export-blocks-set 'org-export-blocks org-export-blocks)))
122 (defcustom org-export-interblocks
124 "Use this a-list to associate block types with block exporting
125 functions. The type of a block is determined by the text
126 immediately following the '#+BEGIN_' portion of the block header.
127 Each block export function should accept three argumets..."
128 :group 'org-export-general
129 :type 'alist)
131 (defcustom org-export-blocks-witheld
132 '(hidden)
133 "List of block types (see `org-export-blocks') which should not
134 be exported."
135 :group 'org-export-general
136 :type 'list)
138 (defvar org-export-blocks-postblock-hooks nil "")
140 (defun org-export-blocks-html-quote (body &optional open close)
141 "Protext BODY from org html export. The optional OPEN and
142 CLOSE tags will be inserted around BODY."
143 (concat
144 "\n#+BEGIN_HTML\n"
145 (or open "")
146 body (if (string-match "\n$" body) "" "\n")
147 (or close "")
148 "#+END_HTML\n"))
150 (defun org-export-blocks-latex-quote (body &optional open close)
151 "Protext BODY from org latex export. The optional OPEN and
152 CLOSE tags will be inserted around BODY."
153 (concat
154 "\n#+BEGIN_LaTeX\n"
155 (or open "")
156 body (if (string-match "\n$" body) "" "\n")
157 (or close "")
158 "#+END_LaTeX\n"))
160 (defun org-export-blocks-preprocess ()
161 "Export all blocks acording to the `org-export-blocks' block
162 exportation alist. Does not export block types specified in
163 specified in BLOCKS which default to the value of
164 `org-export-blocks-witheld'."
165 (interactive)
166 (save-window-excursion
167 (let ((case-fold-search t)
168 (types '())
169 indentation type func start)
170 (flet ((interblock (start end)
171 (mapcar (lambda (pair) (funcall (second pair) start end))
172 org-export-interblocks)))
173 (goto-char (point-min))
174 (setq start (point))
175 (while (re-search-forward
176 "^\\([ \t]*\\)#\\+begin_\\(\\S-+\\)[ \t]*\\(.*\\)?[\r\n]\\([^\000]*?\\)[\r\n][ \t]*#\\+end_\\S-+.*" nil t)
177 (setq indentation (length (match-string 1)))
178 (setq type (intern (match-string 2)))
179 (unless (memq type types) (setq types (cons type types)))
180 (save-match-data (interblock start (match-beginning 0)))
181 (if (setq func (cadr (assoc type org-export-blocks)))
182 (progn
183 (replace-match (save-match-data
184 (if (memq type org-export-blocks-witheld)
186 (apply func (save-match-data (org-remove-indentation (match-string 4)))
187 (split-string (match-string 3) " ")))) t t)
188 ;; indent block
189 (indent-code-rigidly (match-beginning 0) (match-end 0) indentation)))
190 (setq start (match-end 0)))
191 (interblock start (point-max))))))
193 (add-hook 'org-export-preprocess-hook 'org-export-blocks-preprocess)
195 ;;================================================================================
196 ;; type specific functions
198 ;;--------------------------------------------------------------------------------
199 ;; ditaa: create images from ASCII art using the ditaa utility
200 (defvar org-ditaa-jar-path (expand-file-name
201 "ditaa.jar"
202 (file-name-as-directory
203 (expand-file-name
204 "scripts"
205 (file-name-as-directory
206 (expand-file-name
207 "../contrib"
208 (file-name-directory (or load-file-name buffer-file-name)))))))
209 "Path to the ditaa jar executable")
211 (defun org-export-blocks-format-ditaa (body &rest headers)
212 "Pass block BODY to the ditaa utility creating an image.
213 Specify the path at which the image should be saved as the first
214 element of headers, any additional elements of headers will be
215 passed to the ditaa utility as command line arguments."
216 (message "ditaa-formatting...")
217 (let ((out-file (if headers (car headers)))
218 (args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
219 (data-file (make-temp-file "org-ditaa")))
220 (unless (file-exists-p org-ditaa-jar-path)
221 (error (format "Could not find ditaa.jar at %s" org-ditaa-jar-path)))
222 (setq body (if (string-match "^\\([^:\\|:[^ ]\\)" body)
223 body
224 (mapconcat (lambda (x) (substring x (if (> (length x) 1) 2 1)))
225 (org-split-string body "\n")
226 "\n")))
227 (cond
228 ((or htmlp latexp docbookp)
229 (with-temp-file data-file (insert body))
230 (message (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file))
231 (shell-command (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file))
232 (format "\n[[file:%s]]\n" out-file))
233 (t (concat
234 "\n#+BEGIN_EXAMPLE\n"
235 body (if (string-match "\n$" body) "" "\n")
236 "#+END_EXAMPLE\n")))))
238 ;;--------------------------------------------------------------------------------
239 ;; dot: create graphs using the dot graphing language
240 ;; (require the dot executable to be in your path)
241 (defun org-export-blocks-format-dot (body &rest headers)
242 "Pass block BODY to the dot graphing utility creating an image.
243 Specify the path at which the image should be saved as the first
244 element of headers, any additional elements of headers will be
245 passed to the dot utility as command line arguments. Don't
246 forget to specify the output type for the dot command, so if you
247 are exporting to a file with a name like 'image.png' you should
248 include a '-Tpng' argument, and your block should look like the
249 following.
251 #+begin_dot models.png -Tpng
252 digraph data_relationships {
253 \"data_requirement\" [shape=Mrecord, label=\"{DataRequirement|description\lformat\l}\"]
254 \"data_product\" [shape=Mrecord, label=\"{DataProduct|name\lversion\lpoc\lformat\l}\"]
255 \"data_requirement\" -> \"data_product\"
257 #+end_dot"
258 (message "dot-formatting...")
259 (let ((out-file (if headers (car headers)))
260 (args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
261 (data-file (make-temp-file "org-ditaa")))
262 (cond
263 ((or htmlp latexp docbookp)
264 (with-temp-file data-file (insert body))
265 (message (concat "dot " data-file " " args " -o " out-file))
266 (shell-command (concat "dot " data-file " " args " -o " out-file))
267 (format "\n[[file:%s]]\n" out-file))
268 (t (concat
269 "\n#+BEGIN_EXAMPLE\n"
270 body (if (string-match "\n$" body) "" "\n")
271 "#+END_EXAMPLE\n")))))
273 ;;--------------------------------------------------------------------------------
274 ;; comment: export comments in author-specific css-stylable divs
275 (defun org-export-blocks-format-comment (body &rest headers)
276 "Format comment BODY by OWNER and return it formatted for export.
277 Currently, this only does something for HTML export, for all
278 other backends, it converts the comment into an EXAMPLE segment."
279 (let ((owner (if headers (car headers)))
280 (title (if (cdr headers) (mapconcat 'identity (cdr headers) " "))))
281 (cond
282 (htmlp ;; We are exporting to HTML
283 (concat "#+BEGIN_HTML\n"
284 "<div class=\"org-comment\""
285 (if owner (format " id=\"org-comment-%s\" " owner))
286 ">\n"
287 (if owner (concat "<b>" owner "</b> ") "")
288 (if (and title (> (length title) 0)) (concat " -- " title "</br>\n") "</br>\n")
289 "<p>\n"
290 "#+END_HTML\n"
291 body
292 "#+BEGIN_HTML\n"
293 "</p>\n"
294 "</div>\n"
295 "#+END_HTML\n"))
296 (t ;; This is not HTML, so just make it an example.
297 (concat "#+BEGIN_EXAMPLE\n"
298 (if title (concat "Title:" title "\n") "")
299 (if owner (concat "By:" owner "\n") "")
300 body
301 (if (string-match "\n\\'" body) "" "\n")
302 "#+END_EXAMPLE\n")))))
304 (provide 'org-exp-blocks)
306 ;; arch-tag: 1c365fe9-8808-4f72-bb15-0b00f36d8024
307 ;;; org-exp-blocks.el ends here