ob-io.el 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. ;;; ob-io.el --- org-babel functions for Io evaluation
  2. ;; Copyright (C) Andrzej Lichnerowicz
  3. ;; Author: Andrzej Lichnerowicz
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.02
  7. ;;; License:
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; Currently only supports the external execution. No session support yet.
  24. ;; :results output -- runs in scripting mode
  25. ;; :results output repl -- runs in repl mode
  26. ;;; Requirements:
  27. ;; - Io language :: http://iolanguage.org/
  28. ;; - Io major mode :: Can be installed from Io sources
  29. ;; https://github.com/stevedekorte/io/blob/master/extras/SyntaxHighlighters/Emacs/io-mode.el
  30. ;;; Code:
  31. (require 'ob)
  32. (require 'ob-ref)
  33. (require 'ob-comint)
  34. (require 'ob-eval)
  35. (add-to-list 'org-babel-tangle-lang-exts '("io" . "io"))
  36. (defvar org-babel-default-header-args:io '())
  37. (defvar org-babel-io-command "io"
  38. "Name of the command to use for executing Io code.")
  39. (defun org-babel-execute:io (body params)
  40. "Execute a block of Io code with org-babel. This function is
  41. called by `org-babel-execute-src-block'"
  42. (message "executing Io source code block")
  43. (let* ((processed-params (org-babel-process-params params))
  44. (session (org-babel-io-initiate-session (first processed-params)))
  45. (vars (second processed-params))
  46. (result-params (third processed-params))
  47. (result-type (cdr (assoc :result-type params)))
  48. (full-body (org-babel-expand-body:generic
  49. body params))
  50. (result (org-babel-io-evaluate
  51. session full-body result-type result-params)))
  52. (org-babel-reassemble-table
  53. result
  54. (org-babel-pick-name
  55. (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
  56. (org-babel-pick-name
  57. (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))
  58. (defun org-babel-io-table-or-string (results)
  59. "Convert RESULTS into an appropriate elisp value.
  60. If RESULTS look like a table, then convert them into an
  61. Emacs-lisp table, otherwise return the results as a string."
  62. (org-babel-script-escape results))
  63. (defvar org-babel-io-wrapper-method
  64. "(
  65. %s
  66. ) asString print
  67. ")
  68. (defun org-babel-io-evaluate (session body &optional result-type result-params)
  69. "Evaluate BODY in external Io process.
  70. If RESULT-TYPE equals 'output then return standard output as a string.
  71. If RESULT-TYPE equals 'value then return the value of the last statement
  72. in BODY as elisp."
  73. (when session (error "Sessions are not supported for Io. Yet."))
  74. (case result-type
  75. (output
  76. (if (member "repl" result-params)
  77. (org-babel-eval org-babel-io-command body)
  78. (let ((src-file (org-babel-temp-file "io-")))
  79. (progn (with-temp-file src-file (insert full-body))
  80. (org-babel-eval
  81. (concat org-babel-io-command " " src-file) "")))))
  82. (value (let* ((src-file (org-babel-temp-file "io-"))
  83. (wrapper (format org-babel-io-wrapper-method body)))
  84. (with-temp-file src-file (insert wrapper))
  85. ((lambda (raw)
  86. (if (member "code" result-params)
  87. raw
  88. (org-babel-io-table-or-string raw)))
  89. (org-babel-eval
  90. (concat org-babel-io-command " " src-file) ""))))))
  91. (defun org-babel-prep-session:io (session params)
  92. "Prepare SESSION according to the header arguments specified in PARAMS."
  93. (error "Sessions are not supported for Io. Yet."))
  94. (defun org-babel-io-initiate-session (&optional session)
  95. "If there is not a current inferior-process-buffer in SESSION
  96. then create. Return the initialized session. Sessions are not
  97. supported in Io."
  98. nil)
  99. (provide 'ob-io)
  100. ;;; ob-io.el ends here