ob-io.el 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. ;;; ob-io.el --- Babel Functions for Io -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2012-2021 Free Software Foundation, Inc.
  3. ;; Author: Andrzej Lichnerowicz
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: https://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. ;; 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 :: https://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. (defvar org-babel-tangle-lang-exts) ;; Autoloaded
  28. (add-to-list 'org-babel-tangle-lang-exts '("io" . "io"))
  29. (defvar org-babel-default-header-args:io '())
  30. (defvar org-babel-io-command "io"
  31. "Name of the command to use for executing Io code.")
  32. (defun org-babel-execute:io (body params)
  33. "Execute a block of Io code with org-babel.
  34. This function is called by `org-babel-execute-src-block'."
  35. (message "executing Io source code block")
  36. (let* ((processed-params (org-babel-process-params params))
  37. (session (org-babel-io-initiate-session (nth 0 processed-params)))
  38. (result-params (nth 2 processed-params))
  39. (result-type (cdr (assq :result-type params)))
  40. (full-body (org-babel-expand-body:generic
  41. body params))
  42. (result (org-babel-io-evaluate
  43. session full-body result-type result-params)))
  44. (org-babel-reassemble-table
  45. result
  46. (org-babel-pick-name
  47. (cdr (assq :colname-names params)) (cdr (assq :colnames params)))
  48. (org-babel-pick-name
  49. (cdr (assq :rowname-names params)) (cdr (assq :rownames params))))))
  50. (defvar org-babel-io-wrapper-method
  51. "(
  52. %s
  53. ) asString print
  54. ")
  55. (defun org-babel-io-evaluate (session body &optional result-type result-params)
  56. "Evaluate BODY in external Io process.
  57. If RESULT-TYPE equals `output' then return standard output as a string.
  58. If RESULT-TYPE equals `value' then return the value of the last statement
  59. in BODY as elisp."
  60. (when session (error "Sessions are not (yet) supported for Io"))
  61. (pcase result-type
  62. (`output
  63. (if (member "repl" result-params)
  64. (org-babel-eval org-babel-io-command body)
  65. (let ((src-file (org-babel-temp-file "io-")))
  66. (progn (with-temp-file src-file (insert body))
  67. (org-babel-eval
  68. (concat org-babel-io-command " " src-file) "")))))
  69. (`value (let* ((src-file (org-babel-temp-file "io-"))
  70. (wrapper (format org-babel-io-wrapper-method body)))
  71. (with-temp-file src-file (insert wrapper))
  72. (let ((raw (org-babel-eval
  73. (concat org-babel-io-command " " src-file) "")))
  74. (org-babel-result-cond result-params
  75. raw
  76. (org-babel-script-escape raw)))))))
  77. (defun org-babel-prep-session:io (_session _params)
  78. "Prepare SESSION according to the header arguments specified in PARAMS."
  79. (error "Sessions are not (yet) supported for Io"))
  80. (defun org-babel-io-initiate-session (&optional _session)
  81. "If there is not a current inferior-process-buffer in SESSION
  82. then create. Return the initialized session. Sessions are not
  83. supported in Io."
  84. nil)
  85. (provide 'ob-io)
  86. ;;; ob-io.el ends here