ob-clojure.el 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.5
  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. (defvar org-babel-header-arg-names:clojure '(package))
  34. (defun org-babel-expand-body:clojure (body params)
  35. "Expand BODY according to PARAMS, return the expanded body."
  36. (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
  37. (result-params (cdr (assoc :result-params params)))
  38. (print-level nil) (print-length nil)
  39. (body (org-babel-trim
  40. (if (> (length vars) 0)
  41. (concat "(let ["
  42. (mapconcat
  43. (lambda (var)
  44. (format "%S (quote %S)" (car var) (cdr var)))
  45. vars "\n ")
  46. "]\n" body ")")
  47. body))))
  48. (if (or (member "code" result-params)
  49. (member "pp" result-params))
  50. (format (concat "(let [org-mode-print-catcher (java.io.StringWriter.)]"
  51. "(clojure.pprint/with-pprint-dispatch %s-dispatch"
  52. "(clojure.pprint/pprint %s org-mode-print-catcher)"
  53. "(str org-mode-print-catcher)))")
  54. (if (member "code" result-params) "code" "simple") body)
  55. body)))
  56. (defun org-babel-execute:clojure (body params)
  57. "Execute a block of Clojure code with Babel."
  58. (require 'slime) (require 'swank-clojure)
  59. (with-temp-buffer
  60. (insert (org-babel-expand-body:clojure body params))
  61. (read
  62. (slime-eval
  63. `(swank:interactive-eval-region
  64. ,(buffer-substring-no-properties (point-min) (point-max)))
  65. (cdr (assoc :package params))))))
  66. (provide 'ob-clojure)
  67. ;; arch-tag: a43b33f2-653e-46b1-ac56-2805cf05b7d1
  68. ;;; ob-clojure.el ends here