ob-clojure.el 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. ;;; ob-clojure.el --- org-babel functions for clojure evaluation
  2. ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
  3. ;; Author: Joel Boehland, Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 7.3
  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, relies on slime for all eval
  20. ;;; Requirements:
  21. ;;; - clojure (at least 1.2.0)
  22. ;;; - clojure-mode
  23. ;;; - slime
  24. ;;; - swank-clojure
  25. ;;; By far, 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. (declare-function slime-eval "ext:slime" (sexp &optional package))
  31. (add-to-list 'org-babel-tangle-lang-exts '("clojure" . "clj"))
  32. (defvar org-babel-default-header-args:clojure '())
  33. (defun org-babel-expand-body:clojure (body params)
  34. "Expand BODY according to PARAMS, return the expanded body."
  35. (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
  36. (result-params (cdr (assoc :result-params params)))
  37. (print-level nil) (print-length nil)
  38. (body (org-babel-trim
  39. (if (> (length vars) 0)
  40. (concat "(let ["
  41. (mapconcat
  42. (lambda (var)
  43. (format "%S (quote %S)" (car var) (cdr var)))
  44. vars "\n ")
  45. "]\n" body ")")
  46. body))))
  47. (if (or (member "code" result-params)
  48. (member "pp" result-params))
  49. (format (concat "(let [org-mode-print-catcher (java.io.StringWriter.)]"
  50. "(clojure.pprint/with-pprint-dispatch %s-dispatch"
  51. "(clojure.pprint/pprint %s org-mode-print-catcher)"
  52. "(str org-mode-print-catcher)))")
  53. (if (member "code" result-params) "code" "simple") body)
  54. body)))
  55. (defun org-babel-execute:clojure (body params)
  56. "Execute a block of Clojure code with Babel."
  57. (require 'slime) (require 'swank-clojure)
  58. (with-temp-buffer
  59. (insert (org-babel-expand-body:clojure body params))
  60. (read
  61. (slime-eval
  62. `(swank:interactive-eval-region
  63. ,(buffer-substring-no-properties (point-min) (point-max)))
  64. (cdr (assoc :package params))))))
  65. (provide 'ob-clojure)
  66. ;; arch-tag: a43b33f2-653e-46b1-ac56-2805cf05b7d1
  67. ;;; ob-clojure.el ends here