aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/org/ob-processing.el
diff options
context:
space:
mode:
authorRasmus2017-06-21 13:20:20 +0200
committerRasmus2017-06-22 11:54:18 +0200
commit5cecd275820df825c51bf9a27fcc7e35f30ff273 (patch)
treeb3f72e63953613d565e6d5a35bec97f158eb603c /lisp/org/ob-processing.el
parent386a3da920482b8cb3e962fb944d135c8a770e26 (diff)
downloademacs-5cecd275820df825c51bf9a27fcc7e35f30ff273.tar.gz
emacs-5cecd275820df825c51bf9a27fcc7e35f30ff273.zip
Update Org to v9.0.9
Please see etc/ORG-NEWS for details.
Diffstat (limited to 'lisp/org/ob-processing.el')
-rw-r--r--lisp/org/ob-processing.el195
1 files changed, 195 insertions, 0 deletions
diff --git a/lisp/org/ob-processing.el b/lisp/org/ob-processing.el
new file mode 100644
index 00000000000..a18a53cbf1e
--- /dev/null
+++ b/lisp/org/ob-processing.el
@@ -0,0 +1,195 @@
1;;; ob-processing.el --- Babel functions for processing -*- lexical-binding: t; -*-
2
3;; Copyright (C) 2015-2017 Free Software Foundation, Inc.
4
5;; Author: Jarmo Hurri (adapted from ob-asymptote.el written by Eric Schulte)
6;; Keywords: literate programming, reproducible research
7;; Homepage: http://orgmode.org
8
9;; This file is part of GNU Emacs.
10
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.
15
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.
20
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/>.
23
24;;; Commentary:
25
26;; Babel support for evaluating processing source code.
27;;
28;; This differs from most standard languages in that
29;;
30;; 1) there is no such thing as a "session" in processing
31;;
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
38;;
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
43;;
44;; (define-key org-babel-map (kbd "C-k") 'org-babel-processing-view-sketch)
45
46
47;;; Requirements:
48
49;; - processing2-emacs mode :: https://github.com/ptrv/processing2-emacs
50;; - Processing.js module :: http://processingjs.org/
51
52;;; Code:
53(require 'ob)
54(require 'sha1)
55
56(declare-function processing-sketch-run "ext:processing-mode" ())
57
58(defvar org-babel-temporary-directory)
59
60(defvar org-babel-tangle-lang-exts)
61(add-to-list 'org-babel-tangle-lang-exts '("processing" . "pde"))
62
63;; Default header tags depend on whether exporting html or not; if not
64;; exporting html, then no results are produced; otherwise results are
65;; HTML.
66(defvar org-babel-default-header-args:processing
67 '((:results . "html") (:exports . "results"))
68 "Default arguments when evaluating a Processing source block.")
69
70(defvar org-babel-processing-processing-js-filename "processing.js"
71 "Filename of the processing.js file.")
72
73(defun org-babel-processing-view-sketch ()
74 "Show the sketch of the Processing block under point in an external viewer."
75 (interactive)
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)))
81 (sketch-code
82 (org-babel-expand-body:generic
83 body
84 params
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)
92 (sketch-dir
93 (let (sketch-dir-candidate)
94 (while
95 (progn
96 (setq sketch-dir-candidate
97 (make-temp-file "processing" t))
98 (when (string-match-p
99 "-"
100 (file-name-nondirectory sketch-dir-candidate))
101 (delete-directory sketch-dir-candidate)
102 t)))
103 sketch-dir-candidate))
104 (sketch-filename
105 (concat sketch-dir
106 "/"
107 (file-name-nondirectory sketch-dir)
108 ".pde")))
109 (with-temp-file sketch-filename (insert sketch-code))
110 (find-file sketch-filename)
111 (processing-sketch-run)
112 (kill-buffer)))
113 (message "Not inside a Processing source block."))))
114
115(defun org-babel-execute:processing (body params)
116 "Execute a block of Processing code.
117This function is called by `org-babel-execute-src-block'."
118 (let ((sketch-code
119 (org-babel-expand-body:generic
120 body
121 params
122 (org-babel-variable-assignments:processing params))))
123 ;; Results are HTML.
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=\""
129 sketch-canvas-id
130 "\">\n"
131 sketch-code
132 "\n</script> <canvas id=\""
133 sketch-canvas-id
134 "\"></canvas>"))))
135
136(defun org-babel-prep-session:processing (_session _params)
137 "Return an error if the :session header argument is set.
138Processing does not support sessions"
139 (error "Processing does not support sessions"))
140
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)))
145
146(defun org-babel-processing-var-to-processing (pair)
147 "Convert an elisp value into a Processing variable.
148The elisp value PAIR is converted into Processing code specifying
149a variable of the same value."
150 (let ((var (car pair))
151 (val (let ((v (cdr pair)))
152 (if (symbolp v) (symbol-name v) v))))
153 (cond
154 ((integerp val)
155 (format "int %S=%S;" var val))
156 ((floatp val)
157 (format "float %S=%S;" var val))
158 ((stringp 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)))
165 ((listp val)
166 (let* ((type (org-babel-processing-define-type val))
167 (fmt (if (eq 'String type) "\"%s\"" "%s"))
168 (array (mapconcat (lambda (row)
169 (concat "{"
170 (mapconcat (lambda (e) (format fmt e))
171 row ", ")
172 "}"))
173 val ",")))
174 (format "%S[][] %S={%s};" type var array))))))
175
176(defun org-babel-processing-define-type (data)
177 "Determine type of DATA.
178
179DATA is a list. Return type as a symbol.
180
181The type is `String' if any element in DATA is a string.
182Otherwise, it is either `float', if some elements are floats, or
183`int'."
184 (letrec ((type 'int)
185 (find-type
186 (lambda (row)
187 (dolist (e row type)
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))))
192
193(provide 'ob-processing)
194
195;;; ob-processing.el ends here