ob-oz.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. ;;; ob-oz.el --- org-babel functions for Oz evaluation
  2. ;; Copyright (C) 2009, 2012 Torsten Anders and Eric Schulte
  3. ;; Author: Torsten Anders and Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;;; License:
  7. ;; This program 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, or (at your option)
  10. ;; any later version.
  11. ;;
  12. ;; This program is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;;
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  19. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  20. ;; Boston, MA 02110-1301, USA.
  21. ;;; Commentary:
  22. ;; Org-Babel support for evaluating Oz source code.
  23. ;;
  24. ;; Oz code is always send to the Oz Programming Environment (OPI), the
  25. ;; Emacs mode and compiler interface for Oz programs. Therefore, only
  26. ;; session mode is supported. In practice, non-session code blocks are
  27. ;; handled equally well by the session mode. However, only a single
  28. ;; session is supported. Consequently, the :session header argument is
  29. ;; ignored.
  30. ;;
  31. ;; The Org-babel header argument :results is interpreted as
  32. ;; follows. :results output requires the respective code block to be
  33. ;; an Oz statement and :results value requires an Oz
  34. ;; expression. Currently, results are only supported for expressions
  35. ;; (i.e. the result of :results output is always nil).
  36. ;;
  37. ;; Expression evaluation happens synchronously. Therefore there is an
  38. ;; additional header argument :wait-time <number>, which specifies the
  39. ;; maximum time to wait for the result of a given expression. nil
  40. ;; means to wait as long as it takes to get a result (potentially wait
  41. ;; forever).
  42. ;;
  43. ;; NOTE: Currently the copyright of this file may not be in a state to
  44. ;; permit inclusion as core software into Emacs or Org-mode.
  45. ;;; Requirements:
  46. ;; - Mozart Programming System, the implementation of the Oz
  47. ;; programming language (http://www.mozart-oz.org/), which includes
  48. ;; the major mode mozart for editing Oz programs.
  49. ;;
  50. ;; - StartOzServer.oz which is located in the contrib/scripts
  51. ;; directory of the Org-mode repository
  52. ;;; TODO:
  53. ;; - Decide: set communication to \\switch -threadedqueries?
  54. ;;
  55. ;; - Only start Oz compiler when required, e.g., load Org-babel only when needed?
  56. ;;
  57. ;; - Avoid synchronous evaluation to avoid blocking Emacs (complex
  58. ;; Strasheela programs can take long to find a result..). In order
  59. ;; to cleanly map code blocks to their associated results (which can
  60. ;; arrive then in any order) I could use IDs
  61. ;; (e.g. integers). However, how do I do concurrency in Emacs Lisp,
  62. ;; and how can I define org-babel-execute:oz concurrently.
  63. ;;
  64. ;; - Expressions are rarely used in Oz at the top-level, and using
  65. ;; them in documentation and Literate Programs will cause
  66. ;; confusion. Idea: hide expression from reader and instead show
  67. ;; them statement (e.g., MIDI output statement) and then include
  68. ;; result in Org file. Implementation: for expressions (:results
  69. ;; value) support an additional header argument that takes arbitrary
  70. ;; Oz code. This code is not seen by the reader, but will be used
  71. ;; for the actual expression at the end. Alternative: feed all
  72. ;; relevant code as statement (:results output), then add expression
  73. ;; as extra code block which outputs, e.g., file name (so the file
  74. ;; name must be accessible by global var), but the code of this
  75. ;; extra codeblock is not seen. Hm, in that case it might be even
  76. ;; more easy to manually add this link to the Org file.
  77. ;;
  78. (require 'ob)
  79. ;;; major mode for editing Oz programs
  80. (require 'mozart)
  81. ;;
  82. ;; Interface to communicate with Oz.
  83. ;; (1) For statements without any results: oz-send-string
  84. ;; (2) For expressions with a single result: oz-send-string-expression
  85. ;; (defined in org-babel-oz-ResultsValue.el)
  86. ;;
  87. ;; oz-send-string-expression implements an additional very direct
  88. ;; communication between Org-babel and the Oz compiler. Communication
  89. ;; with the Oz server works already without this code via the function
  90. ;; oz-send-string from mozart.el.in, but this function does not get
  91. ;; back any results from Oz to Emacs. The following code creates a
  92. ;; socket for sending code to the OPI compiler and results are
  93. ;; returned by the same socket. On the Oz side, a socket is opened and
  94. ;; conected to the compiler of the OPI (via oz-send-string). On the
  95. ;; Emacs side, a connection to this socket is created for feeding code
  96. ;; and receiving results. This additional communication channel to the
  97. ;; OPI compiler ensures that results are returned cleanly (e.g., only
  98. ;; the result of the sent code is returned, no parsing or any
  99. ;; processing of *Oz Emulator* is required).
  100. ;;
  101. ;; There is no buffer, nor sentinel involved. Oz code is send
  102. ;; directly, and results from Oz are send back, but Emacs Lisp
  103. ;; requires a filter function for processing results.
  104. (defvar org-babel-oz-server-dir
  105. (file-name-as-directory
  106. (expand-file-name
  107. "contrib/scripts"
  108. (file-name-as-directory
  109. (expand-file-name
  110. "../../.."
  111. (file-name-directory (or load-file-name buffer-file-name))))))
  112. "Path to the contrib/scripts directory in which
  113. StartOzServer.oz is located.")
  114. (defvar org-babel-oz-port 6001
  115. "Port for communicating with Oz compiler.")
  116. (defvar org-babel-oz-OPI-socket nil
  117. "Socket for communicating with OPI.")
  118. (defvar org-babel-oz-collected-result nil
  119. "Aux var to hand result from org-babel-oz-filter to oz-send-string-expression.")
  120. (defun org-babel-oz-filter (proc string)
  121. "Processes output from socket org-babel-oz-OPI-socket."
  122. ;; (setq org-babel-oz-collected-results (cons string org-babel-oz-collected-results))
  123. (setq org-babel-oz-collected-result string)
  124. )
  125. (defun org-babel-oz-create-socket ()
  126. (message "Create OPI socket for evaluating expressions")
  127. ;; Start Oz directly
  128. (run-oz)
  129. ;; Create socket on Oz side (after Oz was started).
  130. (oz-send-string (concat "\\insert '" org-babel-oz-server-dir "StartOzServer.oz'"))
  131. ;; Wait until socket is created before connecting to it.
  132. ;; Quick hack: wait 3 sec
  133. ;;
  134. ;; extending time to 30 secs does not help when starting Emacs for
  135. ;; the first time (and computer does nothing else)
  136. (sit-for 3)
  137. ;; connect to OPI socket
  138. (setq org-babel-oz-OPI-socket
  139. ;; Creates a socket. I/O interface of Emacs sockets as for processes.
  140. (open-network-stream "*Org-babel-OPI-socket*" nil "localhost" org-babel-oz-port))
  141. ;; install filter
  142. (set-process-filter org-babel-oz-OPI-socket #'org-babel-oz-filter)
  143. )
  144. ;; communication with org-babel-oz-OPI-socket is asynchronous, but
  145. ;; oz-send-string-expression turns is into synchronous...
  146. (defun oz-send-string-expression (string &optional wait-time)
  147. "Similar to oz-send-string, oz-send-string-expression sends a string to the OPI compiler. However, string must be expression and this function returns the result of the expression (as string). oz-send-string-expression is synchronous, wait-time allows to specify a maximum wait time. After wait-time is over with no result, the function returns nil."
  148. (if (not org-babel-oz-OPI-socket)
  149. (org-babel-oz-create-socket))
  150. (let ((polling-delay 0.1)
  151. result)
  152. (process-send-string org-babel-oz-OPI-socket string)
  153. ;; wait for result
  154. (if wait-time
  155. (let ((waited 0))
  156. (unwind-protect
  157. (progn
  158. (while
  159. ;; stop loop if org-babel-oz-collected-result \= nil or waiting time is over
  160. (not (or (not (equal org-babel-oz-collected-result nil))
  161. (> waited wait-time)))
  162. (progn
  163. (sit-for polling-delay)
  164. ;; (message "org-babel-oz: next polling iteration")
  165. (setq waited (+ waited polling-delay))))
  166. ;; (message "org-babel-oz: waiting over, got result or waiting timed out")
  167. ;; (message (format "wait-time: %s, waited: %s" wait-time waited))
  168. (setq result org-babel-oz-collected-result)
  169. (setq org-babel-oz-collected-result nil))))
  170. (unwind-protect
  171. (progn
  172. (while (equal org-babel-oz-collected-result nil)
  173. (sit-for polling-delay))
  174. (setq result org-babel-oz-collected-result)
  175. (setq org-babel-oz-collected-result nil))))
  176. result))
  177. (defun org-babel-expand-body:oz (body params)
  178. (let ((vars (mapcar #'cdr (org-babel-get-header params :var))))
  179. (if vars
  180. ;; prepend code to define all arguments passed to the code block
  181. (let ((var-string (mapcar (lambda (pair)
  182. (format "%s=%s"
  183. (car pair)
  184. (org-babel-oz-var-to-oz (cdr pair))))
  185. vars)))
  186. ;; only add var declarations if any variables are there
  187. (mapconcat #'identity
  188. (append (list "local") var-string (list "in" body "end"))
  189. "\n"))
  190. body)))
  191. (defun org-babel-execute:oz (body params)
  192. "Execute a block of Oz code with org-babel. This function is
  193. called by `org-babel-execute-src-block' via multiple-value-bind."
  194. (let* ((result-params (cdr (assoc :result-params params)))
  195. (full-body (org-babel-expand-body:oz body params))
  196. (wait-time (plist-get params :wait-time)))
  197. ;; actually execute the source-code block
  198. (org-babel-reassemble-table
  199. (cond
  200. ((member "output" result-params)
  201. (message "Org-babel: executing Oz statement")
  202. (oz-send-string full-body))
  203. ((member "value" result-params)
  204. (message "Org-babel: executing Oz expression")
  205. (oz-send-string-expression full-body (or wait-time 1)))
  206. (t (error "either 'output' or 'results' must be members of :results.")))
  207. (org-babel-pick-name (cdr (assoc :colname-names params))
  208. (cdr (assoc :colnames params)))
  209. (org-babel-pick-name (cdr (assoc :roname-names params))
  210. (cdr (assoc :rownames params))))))
  211. ;; This function should be used to assign any variables in params in
  212. ;; the context of the session environment.
  213. (defun org-babel-prep-session:oz (session params)
  214. "Prepare SESSION according to the header arguments specified in PARAMS."
  215. (error "org-babel-prep-session:oz unimplemented"))
  216. ;; TODO: testing... (copied from org-babel-haskell.el)
  217. ;; (defun org-babel-prep-session:oz (session params)
  218. ;; "Prepare SESSION according to the header arguments specified in PARAMS."
  219. ;; (save-window-excursion
  220. ;; (org-babel-oz-initiate-session session)
  221. ;; (let* ((vars (org-babel-ref-variables params))
  222. ;; (var-lines (mapconcat ;; define any variables
  223. ;; (lambda (pair)
  224. ;; (format "%s=%s"
  225. ;; (car pair)
  226. ;; (org-babel-ruby-var-to-ruby (cdr pair))))
  227. ;; vars "\n"))
  228. ;; (vars-file (concat (make-temp-file "org-babel-oz-vars") ".oz")))
  229. ;; (when vars
  230. ;; (with-temp-buffer
  231. ;; (insert var-lines) (write-file vars-file)
  232. ;; (oz-mode)
  233. ;; ;; (inferior-oz-load-file) ; ??
  234. ;; ))
  235. ;; (current-buffer))))
  236. ;;
  237. ;; TODO: testing... (simplified version of def in org-babel-prep-session:ocaml)
  238. ;;
  239. ;; BUG: does not work yet. Error: ad-Orig-error: buffer none doesn't exist or has no process
  240. ;; UNUSED DEF
  241. (defun org-babel-oz-initiate-session (&optional session params)
  242. "If there is not a current inferior-process-buffer in SESSION
  243. then create. Return the initialized session."
  244. (unless (string= session "none")
  245. ;; TODO: make it possible to have multiple sessions
  246. (save-window-excursion
  247. ;; (run-oz)
  248. (get-buffer oz-compiler-buffer))))
  249. (defun org-babel-oz-var-to-oz (var)
  250. "Convert an elisp var into a string of Oz source code
  251. specifying a var of the same value."
  252. (if (listp var)
  253. ;; (concat "[" (mapconcat #'org-babel-oz-var-to-oz var ", ") "]")
  254. (eval var)
  255. (format "%s" var) ; don't preserve string quotes.
  256. ;; (format "%s" var)
  257. ))
  258. ;; TODO:
  259. (defun org-babel-oz-table-or-string (results)
  260. "If the results look like a table, then convert them into an
  261. Emacs-lisp table, otherwise return the results as a string."
  262. (error "org-babel-oz-table-or-string unimplemented"))
  263. (provide 'ob-oz)
  264. ;;; org-babel-oz.el ends here