ob-io.el 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. (eval-when-compile (require 'cl))
  28. (defvar org-babel-tangle-lang-exts) ;; Autoloaded
  29. (add-to-list 'org-babel-tangle-lang-exts '("io" . "io"))
  30. (defvar org-babel-default-header-args:io '())
  31. (defvar org-babel-io-command "io"
  32. "Name of the command to use for executing Io code.")
  33. (defun org-babel-execute:io (body params)
  34. "Execute a block of Io code with org-babel. This function is
  35. called by `org-babel-execute-src-block'"
  36. (message "executing Io source code block")
  37. (let* ((processed-params (org-babel-process-params params))
  38. (session (org-babel-io-initiate-session (nth 0 processed-params)))
  39. (vars (nth 1 processed-params))
  40. (result-params (nth 2 processed-params))
  41. (result-type (cdr (assoc :result-type params)))
  42. (full-body (org-babel-expand-body:generic
  43. body params))
  44. (result (org-babel-io-evaluate
  45. session full-body result-type result-params)))
  46. (org-babel-reassemble-table
  47. result
  48. (org-babel-pick-name
  49. (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
  50. (org-babel-pick-name
  51. (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))
  52. (defun org-babel-io-table-or-string (results)
  53. "Convert RESULTS into an appropriate elisp value.
  54. If RESULTS look like a table, then convert them into an
  55. Emacs-lisp table, otherwise return the results as a string."
  56. (org-babel-script-escape results))
  57. (defvar org-babel-io-wrapper-method
  58. "(
  59. %s
  60. ) asString print
  61. ")
  62. (defun org-babel-io-evaluate (session body &optional result-type result-params)
  63. "Evaluate BODY in external Io process.
  64. If RESULT-TYPE equals 'output then return standard output as a string.
  65. If RESULT-TYPE equals 'value then return the value of the last statement
  66. in BODY as elisp."
  67. (when session (error "Sessions are not (yet) supported for Io"))
  68. (case result-type
  69. (output
  70. (if (member "repl" result-params)
  71. (org-babel-eval org-babel-io-command body)
  72. (let ((src-file (org-babel-temp-file "io-")))
  73. (progn (with-temp-file src-file (insert body))
  74. (org-babel-eval
  75. (concat org-babel-io-command " " src-file) "")))))
  76. (value (let* ((src-file (org-babel-temp-file "io-"))
  77. (wrapper (format org-babel-io-wrapper-method body)))
  78. (with-temp-file src-file (insert wrapper))
  79. ((lambda (raw)
  80. (org-babel-result-cond result-params
  81. raw
  82. (org-babel-io-table-or-string raw)))
  83. (org-babel-eval
  84. (concat org-babel-io-command " " src-file) ""))))))
  85. (defun org-babel-prep-session:io (session params)
  86. "Prepare SESSION according to the header arguments specified in PARAMS."
  87. (error "Sessions are not (yet) supported for Io"))
  88. (defun org-babel-io-initiate-session (&optional session)
  89. "If there is not a current inferior-process-buffer in SESSION
  90. then create. Return the initialized session. Sessions are not
  91. supported in Io."
  92. nil)
  93. (provide 'ob-io)
  94. ;;; ob-io.el ends here