ob-io.el 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. ;;; ob-io.el --- org-babel functions for Io evaluation
  2. ;; Copyright (C) 2012 Free Software Foundation, Inc.
  3. ;; Author: Andrzej Lichnerowicz
  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. ;; Currently only supports the external execution. No session support yet.
  19. ;; :results output -- runs in scripting mode
  20. ;; :results output repl -- runs in repl mode
  21. ;;; Requirements:
  22. ;; - Io language :: http://iolanguage.org/
  23. ;; - Io major mode :: Can be installed from Io sources
  24. ;; https://github.com/stevedekorte/io/blob/master/extras/SyntaxHighlighters/Emacs/io-mode.el
  25. ;;; Code:
  26. (require 'ob)
  27. (require 'ob-ref)
  28. (require 'ob-comint)
  29. (require 'ob-eval)
  30. (eval-when-compile (require 'cl))
  31. (add-to-list 'org-babel-tangle-lang-exts '("io" . "io"))
  32. (defvar org-babel-default-header-args:io '())
  33. (defvar org-babel-io-command "io"
  34. "Name of the command to use for executing Io code.")
  35. (defun org-babel-execute:io (body params)
  36. "Execute a block of Io code with org-babel. This function is
  37. called by `org-babel-execute-src-block'"
  38. (message "executing Io source code block")
  39. (let* ((processed-params (org-babel-process-params params))
  40. (session (org-babel-io-initiate-session (nth 0 processed-params)))
  41. (vars (nth 1 processed-params))
  42. (result-params (nth 2 processed-params))
  43. (result-type (cdr (assoc :result-type params)))
  44. (full-body (org-babel-expand-body:generic
  45. body params))
  46. (result (org-babel-io-evaluate
  47. session full-body result-type result-params)))
  48. (org-babel-reassemble-table
  49. result
  50. (org-babel-pick-name
  51. (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
  52. (org-babel-pick-name
  53. (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))
  54. (defun org-babel-io-table-or-string (results)
  55. "Convert RESULTS into an appropriate elisp value.
  56. If RESULTS look like a table, then convert them into an
  57. Emacs-lisp table, otherwise return the results as a string."
  58. (org-babel-script-escape results))
  59. (defvar org-babel-io-wrapper-method
  60. "(
  61. %s
  62. ) asString print
  63. ")
  64. (defun org-babel-io-evaluate (session body &optional result-type result-params)
  65. "Evaluate BODY in external Io process.
  66. If RESULT-TYPE equals 'output then return standard output as a string.
  67. If RESULT-TYPE equals 'value then return the value of the last statement
  68. in BODY as elisp."
  69. (when session (error "Sessions are not supported for Io. Yet."))
  70. (case result-type
  71. (output
  72. (if (member "repl" result-params)
  73. (org-babel-eval org-babel-io-command body)
  74. (let ((src-file (org-babel-temp-file "io-")))
  75. (progn (with-temp-file src-file (insert body))
  76. (org-babel-eval
  77. (concat org-babel-io-command " " src-file) "")))))
  78. (value (let* ((src-file (org-babel-temp-file "io-"))
  79. (wrapper (format org-babel-io-wrapper-method body)))
  80. (with-temp-file src-file (insert wrapper))
  81. ((lambda (raw)
  82. (if (member "code" result-params)
  83. raw
  84. (org-babel-io-table-or-string raw)))
  85. (org-babel-eval
  86. (concat org-babel-io-command " " src-file) ""))))))
  87. (defun org-babel-prep-session:io (session params)
  88. "Prepare SESSION according to the header arguments specified in PARAMS."
  89. (error "Sessions are not supported for Io. Yet."))
  90. (defun org-babel-io-initiate-session (&optional session)
  91. "If there is not a current inferior-process-buffer in SESSION
  92. then create. Return the initialized session. Sessions are not
  93. supported in Io."
  94. nil)
  95. (provide 'ob-io)
  96. ;;; ob-io.el ends here