ob-clojure.el 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. ;;; ob-clojure.el --- org-babel functions for clojure evaluation
  2. ;; Copyright (C) 2009-2015 Free Software Foundation, Inc.
  3. ;; Author: Joel Boehland, Eric Schulte, Oleh Krehel
  4. ;;
  5. ;; Keywords: literate programming, reproducible research
  6. ;; Homepage: http://orgmode.org
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs 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 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs 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. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Support for evaluating clojure code
  20. ;; Requirements:
  21. ;; - clojure (at least 1.2.0)
  22. ;; - clojure-mode
  23. ;; - either cider or SLIME
  24. ;; For Cider, see https://github.com/clojure-emacs/cider
  25. ;; For SLIME, the best way to install these components is by following
  26. ;; the directions as set out by Phil Hagelberg (Technomancy) on the
  27. ;; web page: http://technomancy.us/126
  28. ;;; Code:
  29. (require 'ob)
  30. (eval-when-compile
  31. (require 'cl))
  32. (declare-function nrepl-dict-get "ext:nrepl-client" (dict key))
  33. (declare-function nrepl-sync-request:eval "ext:nrepl-client"
  34. (input connection session &optional ns))
  35. (declare-function slime-eval "ext:slime" (sexp &optional package))
  36. (defvar org-babel-tangle-lang-exts)
  37. (add-to-list 'org-babel-tangle-lang-exts '("clojure" . "clj"))
  38. (defvar org-babel-default-header-args:clojure '())
  39. (defvar org-babel-header-args:clojure '((package . :any)))
  40. (defcustom org-babel-clojure-backend
  41. (cond ((featurep 'cider) 'cider)
  42. (t 'slime))
  43. "Backend used to evaluate Clojure code blocks."
  44. :group 'org-babel
  45. :type '(choice
  46. (const :tag "cider" cider)
  47. (const :tag "SLIME" slime)))
  48. (defun org-babel-expand-body:clojure (body params)
  49. "Expand BODY according to PARAMS, return the expanded body."
  50. (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
  51. (result-params (cdr (assoc :result-params params)))
  52. (print-level nil) (print-length nil)
  53. (body (org-babel-trim
  54. (if (> (length vars) 0)
  55. (concat "(let ["
  56. (mapconcat
  57. (lambda (var)
  58. (format "%S (quote %S)" (car var) (cdr var)))
  59. vars "\n ")
  60. "]\n" body ")")
  61. body))))
  62. (if (or (member "code" result-params)
  63. (member "pp" result-params))
  64. (format "(clojure.pprint/pprint (do %s))" body)
  65. body)))
  66. (defun org-babel-execute:clojure (body params)
  67. "Execute a block of Clojure code with Babel."
  68. (let ((expanded (org-babel-expand-body:clojure body params))
  69. result)
  70. (case org-babel-clojure-backend
  71. (cider
  72. (require 'cider)
  73. (let ((result-params (cdr (assoc :result-params params))))
  74. (setq result
  75. (nrepl-dict-get
  76. (nrepl-sync-request:eval
  77. expanded (cider-current-connection) (cider-current-session))
  78. (if (or (member "output" result-params)
  79. (member "pp" result-params))
  80. "out"
  81. "value")))))
  82. (slime
  83. (require 'slime)
  84. (with-temp-buffer
  85. (insert expanded)
  86. (setq result
  87. (slime-eval
  88. `(swank:eval-and-grab-output
  89. ,(buffer-substring-no-properties (point-min) (point-max)))
  90. (cdr (assoc :package params)))))))
  91. (org-babel-result-cond (cdr (assoc :result-params params))
  92. result
  93. (condition-case nil (org-babel-script-escape result)
  94. (error result)))))
  95. (provide 'ob-clojure)
  96. ;;; ob-clojure.el ends here