ob-clojure.el 3.0 KB

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