Factorize tags parsing
[org-mode.git] / lisp / ob-processing.el
blobdfc4693e8884f1f7c2d9f7afe38d8ee8cddd7b57
1 ;;; ob-processing.el --- Babel functions for processing -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2015-2016 Free Software Foundation, Inc.
5 ;; Author: Jarmo Hurri (adapted from ob-asymptote.el written by Eric Schulte)
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Babel support for evaluating processing source code.
28 ;; This differs from most standard languages in that
30 ;; 1) there is no such thing as a "session" in processing
32 ;; 2) results can only be exported as html; in this case, the
33 ;; processing code is embedded via a file into a javascript block
34 ;; using the processing.js module; the script then draws the
35 ;; resulting output when the web page is viewed in a browser; note
36 ;; that the user is responsible for making sure that processing.js
37 ;; is available on the website
39 ;; 3) it is possible to interactively view the sketch of the
40 ;; Processing code block via Processing 2.0 Emacs mode, using
41 ;; `org-babel-processing-view-sketch'. You can bind this command
42 ;; to, e.g., C-c C-v C-k with
44 ;; (define-key org-babel-map (kbd "C-k") 'org-babel-processing-view-sketch)
47 ;;; Requirements:
49 ;; - processing2-emacs mode :: https://github.com/ptrv/processing2-emacs
50 ;; - Processing.js module :: http://processingjs.org/
52 ;;; Code:
53 (require 'ob)
54 (require 'sha1)
55 (eval-when-compile (require 'cl))
57 (declare-function processing-sketch-run "ext:processing-mode" ())
59 (defvar org-babel-temporary-directory)
61 (defvar org-babel-tangle-lang-exts)
62 (add-to-list 'org-babel-tangle-lang-exts '("processing" . "pde"))
64 ;; Default header tags depend on whether exporting html or not; if not
65 ;; exporting html, then no results are produced; otherwise results are
66 ;; HTML.
67 (defvar org-babel-default-header-args:processing
68 '((:results . "html") (:exports . "results"))
69 "Default arguments when evaluating a Processing source block.")
71 (defvar org-babel-processing-processing-js-filename "processing.js"
72 "Filename of the processing.js file.")
74 (defun org-babel-processing-view-sketch ()
75 "Show the sketch of the Processing block under point in an external viewer."
76 (interactive)
77 (require 'processing-mode)
78 (let ((info (org-babel-get-src-block-info)))
79 (if (string= (nth 0 info) "processing")
80 (let* ((body (nth 1 info))
81 (params (org-babel-process-params (nth 2 info)))
82 (sketch-code
83 (org-babel-expand-body:generic
84 body
85 params
86 (org-babel-variable-assignments:processing params))))
87 ;; Note: sketch filename can not contain a hyphen, since it
88 ;; has to be a valid java class name; for this reason
89 ;; make-temp-file is repeated until no hyphen is in the
90 ;; name; also sketch dir name must be the same as the
91 ;; basename of the sketch file.
92 (let* ((temporary-file-directory org-babel-temporary-directory)
93 (sketch-dir
94 (let (sketch-dir-candidate)
95 (while
96 (progn
97 (setq sketch-dir-candidate
98 (make-temp-file "processing" t))
99 (when (org-string-match-p
101 (file-name-nondirectory sketch-dir-candidate))
102 (delete-directory sketch-dir-candidate)
103 t)))
104 sketch-dir-candidate))
105 (sketch-filename
106 (concat sketch-dir
108 (file-name-nondirectory sketch-dir)
109 ".pde")))
110 (with-temp-file sketch-filename (insert sketch-code))
111 (find-file sketch-filename)
112 (processing-sketch-run)
113 (kill-buffer)))
114 (message "Not inside a Processing source block."))))
116 (defun org-babel-execute:processing (body params)
117 "Execute a block of Processing code.
118 This function is called by `org-babel-execute-src-block'."
119 (let ((sketch-code
120 (org-babel-expand-body:generic
121 body
122 params
123 (org-babel-variable-assignments:processing params))))
124 ;; Results are HTML.
125 (let ((sketch-canvas-id (concat "ob-" (sha1 sketch-code))))
126 (concat "<script src=\""
127 org-babel-processing-processing-js-filename
128 "\"></script>\n <script type=\"text/processing\""
129 " data-processing-target=\""
130 sketch-canvas-id
131 "\">\n"
132 sketch-code
133 "\n</script> <canvas id=\""
134 sketch-canvas-id
135 "\"></canvas>"))))
137 (defun org-babel-prep-session:processing (_session _params)
138 "Return an error if the :session header argument is set.
139 Processing does not support sessions"
140 (error "Processing does not support sessions"))
142 (defun org-babel-variable-assignments:processing (params)
143 "Return list of processing statements assigning the block's variables."
144 (mapcar #'org-babel-processing-var-to-processing
145 (org-babel--get-vars params)))
147 (defun org-babel-processing-var-to-processing (pair)
148 "Convert an elisp value into a Processing variable.
149 The elisp value PAIR is converted into Processing code specifying
150 a variable of the same value."
151 (let ((var (car pair))
152 (val (let ((v (cdr pair)))
153 (if (symbolp v) (symbol-name v) v))))
154 (cond
155 ((integerp val)
156 (format "int %S=%S;" var val))
157 ((floatp val)
158 (format "float %S=%S;" var val))
159 ((stringp val)
160 (format "String %S=\"%s\";" var val))
161 ((and (listp val) (not (listp (car val))))
162 (let* ((type (org-babel-processing-define-type val))
163 (fmt (if (eq 'String type) "\"%s\"" "%s"))
164 (vect (mapconcat (lambda (e) (format fmt e)) val ", ")))
165 (format "%s[] %S={%s};" type var vect)))
166 ((listp val)
167 (let* ((type (org-babel-processing-define-type val))
168 (fmt (if (eq 'String type) "\"%s\"" "%s"))
169 (array (mapconcat (lambda (row)
170 (concat "{"
171 (mapconcat (lambda (e) (format fmt e))
172 row ", ")
173 "}"))
174 val ",")))
175 (format "%S[][] %S={%s};" type var array))))))
177 (defun org-babel-processing-define-type (data)
178 "Determine type of DATA.
180 DATA is a list. Return type as a symbol.
182 The type is `String' if any element in DATA is a string.
183 Otherwise, it is either `float', if some elements are floats, or
184 `int'."
185 (letrec ((type 'int)
186 (find-type
187 (lambda (row)
188 (dolist (e row type)
189 (cond ((listp e) (setq type (funcall find-type e)))
190 ((stringp e) (throw 'exit 'String))
191 ((floatp e) (setq type 'float)))))))
192 (catch 'exit (funcall find-type data))))
194 (provide 'ob-processing)
196 ;;; ob-processing.el ends here