1 ;;; ob-processing.el --- Babel functions for processing -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2015-2017 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 <https://www.gnu.org/licenses/>.
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)
49 ;; - processing2-emacs mode :: https://github.com/ptrv/processing2-emacs
50 ;; - Processing.js module :: http://processingjs.org/
56 (declare-function processing-sketch-run
"ext:processing-mode" ())
58 (defvar org-babel-temporary-directory
)
60 (defvar org-babel-tangle-lang-exts
)
61 (add-to-list 'org-babel-tangle-lang-exts
'("processing" .
"pde"))
63 ;; Default header tags depend on whether exporting html or not; if not
64 ;; exporting html, then no results are produced; otherwise results are
66 (defvar org-babel-default-header-args
:processing
67 '((:results .
"html") (:exports .
"results"))
68 "Default arguments when evaluating a Processing source block.")
70 (defvar org-babel-processing-processing-js-filename
"processing.js"
71 "Filename of the processing.js file.")
73 (defun org-babel-processing-view-sketch ()
74 "Show the sketch of the Processing block under point in an external viewer."
76 (require 'processing-mode
)
77 (let ((info (org-babel-get-src-block-info)))
78 (if (string= (nth 0 info
) "processing")
79 (let* ((body (nth 1 info
))
80 (params (org-babel-process-params (nth 2 info
)))
82 (org-babel-expand-body:generic
85 (org-babel-variable-assignments:processing params
))))
86 ;; Note: sketch filename can not contain a hyphen, since it
87 ;; has to be a valid java class name; for this reason
88 ;; make-temp-file is repeated until no hyphen is in the
89 ;; name; also sketch dir name must be the same as the
90 ;; basename of the sketch file.
91 (let* ((temporary-file-directory org-babel-temporary-directory
)
93 (let (sketch-dir-candidate)
96 (setq sketch-dir-candidate
97 (make-temp-file "processing" t
))
100 (file-name-nondirectory sketch-dir-candidate
))
101 (delete-directory sketch-dir-candidate
)
103 sketch-dir-candidate
))
107 (file-name-nondirectory sketch-dir
)
109 (with-temp-file sketch-filename
(insert sketch-code
))
110 (find-file sketch-filename
)
111 (processing-sketch-run)
113 (message "Not inside a Processing source block."))))
115 (defun org-babel-execute:processing
(body params
)
116 "Execute a block of Processing code.
117 This function is called by `org-babel-execute-src-block'."
119 (org-babel-expand-body:generic
122 (org-babel-variable-assignments:processing params
))))
124 (let ((sketch-canvas-id (concat "ob-" (sha1 sketch-code
))))
125 (concat "<script src=\""
126 org-babel-processing-processing-js-filename
127 "\"></script>\n <script type=\"text/processing\""
128 " data-processing-target=\""
132 "\n</script> <canvas id=\""
136 (defun org-babel-prep-session:processing
(_session _params
)
137 "Return an error if the :session header argument is set.
138 Processing does not support sessions"
139 (error "Processing does not support sessions"))
141 (defun org-babel-variable-assignments:processing
(params)
142 "Return list of processing statements assigning the block's variables."
143 (mapcar #'org-babel-processing-var-to-processing
144 (org-babel--get-vars params
)))
146 (defun org-babel-processing-var-to-processing (pair)
147 "Convert an elisp value into a Processing variable.
148 The elisp value PAIR is converted into Processing code specifying
149 a variable of the same value."
150 (let ((var (car pair
))
151 (val (let ((v (cdr pair
)))
152 (if (symbolp v
) (symbol-name v
) v
))))
155 (format "int %S=%S;" var val
))
157 (format "float %S=%S;" var val
))
159 (format "String %S=\"%s\";" var val
))
160 ((and (listp val
) (not (listp (car val
))))
161 (let* ((type (org-babel-processing-define-type val
))
162 (fmt (if (eq 'String type
) "\"%s\"" "%s"))
163 (vect (mapconcat (lambda (e) (format fmt e
)) val
", ")))
164 (format "%s[] %S={%s};" type var vect
)))
166 (let* ((type (org-babel-processing-define-type val
))
167 (fmt (if (eq 'String type
) "\"%s\"" "%s"))
168 (array (mapconcat (lambda (row)
170 (mapconcat (lambda (e) (format fmt e
))
174 (format "%S[][] %S={%s};" type var array
))))))
176 (defun org-babel-processing-define-type (data)
177 "Determine type of DATA.
179 DATA is a list. Return type as a symbol.
181 The type is `String' if any element in DATA is a string.
182 Otherwise, it is either `float', if some elements are floats, or
188 (cond ((listp e
) (setq type
(funcall find-type e
)))
189 ((stringp e
) (throw 'exit
'String
))
190 ((floatp e
) (setq type
'float
)))))))
191 (catch 'exit
(funcall find-type data
))))
193 (provide 'ob-processing
)
195 ;;; ob-processing.el ends here