ob-clojure.el 3.8 KB

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