ob-processing.el 6.8 KB

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