ob-clojure.el 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. ;;; ob-clojure.el --- org-babel functions for clojure evaluation
  2. ;; Copyright (C) 2009-2014 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-send-string-sync "ext:nrepl-client" (input &optional ns session))
  33. (declare-function slime-eval "ext:slime" (sexp &optional package))
  34. (defvar org-babel-tangle-lang-exts)
  35. (add-to-list 'org-babel-tangle-lang-exts '("clojure" . "clj"))
  36. (defvar org-babel-default-header-args:clojure '())
  37. (defvar org-babel-header-args:clojure '((package . :any)))
  38. (defcustom org-babel-clojure-backend
  39. (cond ((featurep 'cider) 'cider)
  40. (t 'slime))
  41. "Backend used to evaluate Clojure code blocks."
  42. :group 'org-babel
  43. :type '(choice
  44. (const :tag "cider" cider)
  45. (const :tag "SLIME" slime)))
  46. (defun org-babel-expand-body:clojure (body params)
  47. "Expand BODY according to PARAMS, return the expanded body."
  48. (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
  49. (result-params (cdr (assoc :result-params params)))
  50. (print-level nil) (print-length nil)
  51. (body (org-babel-trim
  52. (if (> (length vars) 0)
  53. (concat "(let ["
  54. (mapconcat
  55. (lambda (var)
  56. (format "%S (quote %S)" (car var) (cdr var)))
  57. vars "\n ")
  58. "]\n" body ")")
  59. body))))
  60. (if (or (member "code" result-params)
  61. (member "pp" result-params))
  62. (format "(clojure.pprint/pprint (do %s))" body)
  63. body)))
  64. (defun org-babel-execute:clojure (body params)
  65. "Execute a block of Clojure code with Babel."
  66. (let ((expanded (org-babel-expand-body:clojure body params))
  67. result)
  68. (case org-babel-clojure-backend
  69. (cider
  70. (require 'cider)
  71. (let ((result-params (cdr (assoc :result-params params))))
  72. (setq result
  73. (plist-get
  74. (nrepl-send-string-sync expanded)
  75. (if (or (member "output" result-params)
  76. (member "pp" result-params))
  77. :stdout
  78. :value)))))
  79. (slime
  80. (require 'slime)
  81. (with-temp-buffer
  82. (insert expanded)
  83. (setq result
  84. (slime-eval
  85. `(swank:eval-and-grab-output
  86. ,(buffer-substring-no-properties (point-min) (point-max)))
  87. (cdr (assoc :package params)))))))
  88. (org-babel-result-cond (cdr (assoc :result-params params))
  89. result
  90. (condition-case nil (org-babel-script-escape result)
  91. (error result)))))
  92. (provide 'ob-clojure)
  93. ;;; ob-clojure.el ends here