ob-clojure.el 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. ;;; ob-clojure.el --- Babel Functions for Clojure -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2017 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 'cl-lib)
  30. (require 'ob)
  31. (declare-function cider-current-connection "ext:cider-client" (&optional type))
  32. (declare-function cider-current-session "ext:cider-client" ())
  33. (declare-function nrepl-dict-get "ext:nrepl-client" (dict key))
  34. (declare-function nrepl-sync-request:eval "ext:nrepl-client"
  35. (input connection session &optional ns))
  36. (declare-function org-trim "org" (s &optional keep-lead))
  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 (assq :result-params params)))
  54. (print-level nil) (print-length nil)
  55. (body (org-trim
  56. (if (null vars) (org-trim body)
  57. (concat "(let ["
  58. (mapconcat
  59. (lambda (var)
  60. (format "%S (quote %S)" (car var) (cdr var)))
  61. vars "\n ")
  62. "]\n" body ")")))))
  63. (if (or (member "code" result-params)
  64. (member "pp" result-params))
  65. (format "(clojure.pprint/pprint (do %s))" body)
  66. body)))
  67. (defun org-babel-execute:clojure (body params)
  68. "Execute a block of Clojure code with Babel."
  69. (let ((expanded (org-babel-expand-body:clojure body params))
  70. result)
  71. (cl-case org-babel-clojure-backend
  72. (cider
  73. (require 'cider)
  74. (let ((result-params (cdr (assq :result-params params))))
  75. (setq result
  76. (nrepl-dict-get
  77. (nrepl-sync-request:eval
  78. expanded (cider-current-connection) (cider-current-session))
  79. (if (or (member "output" result-params)
  80. (member "pp" result-params))
  81. "out"
  82. "value")))))
  83. (slime
  84. (require 'slime)
  85. (with-temp-buffer
  86. (insert expanded)
  87. (setq result
  88. (slime-eval
  89. `(swank:eval-and-grab-output
  90. ,(buffer-substring-no-properties (point-min) (point-max)))
  91. (cdr (assq :package params)))))))
  92. (org-babel-result-cond (cdr (assq :result-params params))
  93. result
  94. (condition-case nil (org-babel-script-escape result)
  95. (error result)))))
  96. (provide 'ob-clojure)
  97. ;;; ob-clojure.el ends here