ob-clojure.el 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. ;;; ob-clojure.el --- org-babel functions for clojure evaluation
  2. ;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
  3. ;; Author: Joel Boehland
  4. ;; Eric Schulte
  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, relies on slime for all eval
  20. ;;; Requirements:
  21. ;;; - clojure (at least 1.2.0)
  22. ;;; - clojure-mode
  23. ;;; - slime
  24. ;;; By far, the best way to install these components is by following
  25. ;;; the directions as set out by Phil Hagelberg (Technomancy) on the
  26. ;;; web page: http://technomancy.us/126
  27. ;;; Code:
  28. (require 'ob)
  29. (declare-function slime-eval "ext:slime" (sexp &optional package))
  30. (defvar org-babel-tangle-lang-exts)
  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. (cond ((or (member "code" result-params) (member "pp" result-params))
  49. (format (concat "(let [org-mode-print-catcher (java.io.StringWriter.)] "
  50. "(clojure.pprint/with-pprint-dispatch clojure.pprint/%s-dispatch "
  51. "(clojure.pprint/pprint (do %s) org-mode-print-catcher) "
  52. "(str org-mode-print-catcher)))")
  53. (if (member "code" result-params) "code" "simple") body))
  54. ;; if (:results output), collect printed output
  55. ((member "output" result-params)
  56. (format "(clojure.core/with-out-str %s)" body))
  57. (t body))))
  58. (defun org-babel-execute:clojure (body params)
  59. "Execute a block of Clojure code with Babel."
  60. (require 'slime)
  61. (with-temp-buffer
  62. (insert (org-babel-expand-body:clojure body params))
  63. ((lambda (result)
  64. (let ((result-params (cdr (assoc :result-params params))))
  65. (if (or (member "scalar" result-params)
  66. (member "verbatim" result-params))
  67. result
  68. (condition-case nil (org-babel-script-escape result)
  69. (error result)))))
  70. (slime-eval
  71. `(swank:eval-and-grab-output
  72. ,(buffer-substring-no-properties (point-min) (point-max)))
  73. (cdr (assoc :package params))))))
  74. (provide 'ob-clojure)
  75. ;;; ob-clojure.el ends here