ob-processing.el 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. ;;; ob-processing.el --- Babel functions for processing -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2015-2018 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 <https://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. (declare-function processing-sketch-run "ext:processing-mode" ())
  44. (defvar org-babel-temporary-directory)
  45. (defvar org-babel-tangle-lang-exts)
  46. (add-to-list 'org-babel-tangle-lang-exts '("processing" . "pde"))
  47. ;; Default header tags depend on whether exporting html or not; if not
  48. ;; exporting html, then no results are produced; otherwise results are
  49. ;; HTML.
  50. (defvar org-babel-default-header-args:processing
  51. '((:results . "html") (:exports . "results"))
  52. "Default arguments when evaluating a Processing source block.")
  53. (defvar org-babel-processing-processing-js-filename "processing.js"
  54. "Filename of the processing.js file.")
  55. (defun org-babel-processing-view-sketch ()
  56. "Show the sketch of the Processing block under point in an external viewer."
  57. (interactive)
  58. (require 'processing-mode)
  59. (let ((info (org-babel-get-src-block-info)))
  60. (if (string= (nth 0 info) "processing")
  61. (let* ((body (nth 1 info))
  62. (params (org-babel-process-params (nth 2 info)))
  63. (sketch-code
  64. (org-babel-expand-body:generic
  65. body
  66. params
  67. (org-babel-variable-assignments:processing params))))
  68. ;; Note: sketch filename can not contain a hyphen, since it
  69. ;; has to be a valid java class name; for this reason
  70. ;; make-temp-file is repeated until no hyphen is in the
  71. ;; name; also sketch dir name must be the same as the
  72. ;; basename of the sketch file.
  73. (let* ((temporary-file-directory org-babel-temporary-directory)
  74. (sketch-dir
  75. (let (sketch-dir-candidate)
  76. (while
  77. (progn
  78. (setq sketch-dir-candidate
  79. (make-temp-file "processing" t))
  80. (when (string-match-p
  81. "-"
  82. (file-name-nondirectory sketch-dir-candidate))
  83. (delete-directory sketch-dir-candidate)
  84. t)))
  85. sketch-dir-candidate))
  86. (sketch-filename
  87. (concat sketch-dir
  88. "/"
  89. (file-name-nondirectory sketch-dir)
  90. ".pde")))
  91. (with-temp-file sketch-filename (insert sketch-code))
  92. (find-file sketch-filename)
  93. (processing-sketch-run)
  94. (kill-buffer)))
  95. (message "Not inside a Processing source block."))))
  96. (defun org-babel-execute:processing (body params)
  97. "Execute a block of Processing code.
  98. This function is called by `org-babel-execute-src-block'."
  99. (let ((sketch-code
  100. (org-babel-expand-body:generic
  101. body
  102. params
  103. (org-babel-variable-assignments:processing params))))
  104. ;; Results are HTML.
  105. (let ((sketch-canvas-id (concat "ob-" (sha1 sketch-code))))
  106. (concat "<script src=\""
  107. org-babel-processing-processing-js-filename
  108. "\"></script>\n <script type=\"text/processing\""
  109. " data-processing-target=\""
  110. sketch-canvas-id
  111. "\">\n"
  112. sketch-code
  113. "\n</script> <canvas id=\""
  114. sketch-canvas-id
  115. "\"></canvas>"))))
  116. (defun org-babel-prep-session:processing (_session _params)
  117. "Return an error if the :session header argument is set.
  118. Processing does not support sessions"
  119. (error "Processing does not support sessions"))
  120. (defun org-babel-variable-assignments:processing (params)
  121. "Return list of processing statements assigning the block's variables."
  122. (mapcar #'org-babel-processing-var-to-processing
  123. (org-babel--get-vars params)))
  124. (defun org-babel-processing-var-to-processing (pair)
  125. "Convert an elisp value into a Processing variable.
  126. The elisp value PAIR is converted into Processing code specifying
  127. a variable of the same value."
  128. (let ((var (car pair))
  129. (val (let ((v (cdr pair)))
  130. (if (symbolp v) (symbol-name v) v))))
  131. (cond
  132. ((integerp val)
  133. (format "int %S=%S;" var val))
  134. ((floatp val)
  135. (format "float %S=%S;" var val))
  136. ((stringp val)
  137. (format "String %S=\"%s\";" var val))
  138. ((and (listp val) (not (listp (car val))))
  139. (let* ((type (org-babel-processing-define-type val))
  140. (fmt (if (eq 'String type) "\"%s\"" "%s"))
  141. (vect (mapconcat (lambda (e) (format fmt e)) val ", ")))
  142. (format "%s[] %S={%s};" type var vect)))
  143. ((listp val)
  144. (let* ((type (org-babel-processing-define-type val))
  145. (fmt (if (eq 'String type) "\"%s\"" "%s"))
  146. (array (mapconcat (lambda (row)
  147. (concat "{"
  148. (mapconcat (lambda (e) (format fmt e))
  149. row ", ")
  150. "}"))
  151. val ",")))
  152. (format "%S[][] %S={%s};" type var array))))))
  153. (defun org-babel-processing-define-type (data)
  154. "Determine type of DATA.
  155. DATA is a list. Return type as a symbol.
  156. The type is `String' if any element in DATA is a string.
  157. Otherwise, it is either `float', if some elements are floats, or
  158. `int'."
  159. (letrec ((type 'int)
  160. (find-type
  161. (lambda (row)
  162. (dolist (e row type)
  163. (cond ((listp e) (setq type (funcall find-type e)))
  164. ((stringp e) (throw 'exit 'String))
  165. ((floatp e) (setq type 'float)))))))
  166. (catch 'exit (funcall find-type data))))
  167. (provide 'ob-processing)
  168. ;;; ob-processing.el ends here