Preserve file local variables during some operations
[org-mode/org-tableheadings.git] / lisp / ob-processing.el
blob4e09abc98bcf4df71191608763395be9cb2d53f7
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/>.
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)
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
65 ;; HTML.
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."
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
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."))))
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'."
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>"))))
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))))
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))))))
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
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))))
193 (provide 'ob-processing)
195 ;;; ob-processing.el ends here