Commit | Line | Data |
---|---|---|
86fbb8ca CD |
1 | ;;; ob-clojure.el --- org-babel functions for clojure evaluation |
2 | ||
b73f1974 | 3 | ;; Copyright (C) 2009-2012 Free Software Foundation, Inc. |
86fbb8ca | 4 | |
c7557a0f GM |
5 | ;; Author: Joel Boehland |
6 | ;; Eric Schulte | |
86fbb8ca CD |
7 | ;; Keywords: literate programming, reproducible research |
8 | ;; Homepage: http://orgmode.org | |
86fbb8ca CD |
9 | |
10 | ;; This file is part of GNU Emacs. | |
11 | ||
12 | ;; GNU Emacs is free software: you can redistribute it and/or modify | |
13 | ;; it under the terms of the GNU General Public License as published by | |
14 | ;; the Free Software Foundation, either version 3 of the License, or | |
15 | ;; (at your option) any later version. | |
16 | ||
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | |
24 | ||
25 | ;;; Commentary: | |
26 | ||
acedf35c | 27 | ;;; support for evaluating clojure code, relies on slime for all eval |
86fbb8ca CD |
28 | |
29 | ;;; Requirements: | |
30 | ||
acedf35c CD |
31 | ;;; - clojure (at least 1.2.0) |
32 | ;;; - clojure-mode | |
33 | ;;; - slime | |
34 | ;;; - swank-clojure | |
86fbb8ca CD |
35 | |
36 | ;;; By far, the best way to install these components is by following | |
37 | ;;; the directions as set out by Phil Hagelberg (Technomancy) on the | |
38 | ;;; web page: http://technomancy.us/126 | |
39 | ||
40 | ;;; Code: | |
41 | (require 'ob) | |
86fbb8ca | 42 | |
86fbb8ca | 43 | (declare-function slime-eval "ext:slime" (sexp &optional package)) |
86fbb8ca | 44 | |
3ab2c837 | 45 | (defvar org-babel-tangle-lang-exts) |
86fbb8ca CD |
46 | (add-to-list 'org-babel-tangle-lang-exts '("clojure" . "clj")) |
47 | ||
48 | (defvar org-babel-default-header-args:clojure '()) | |
acedf35c | 49 | (defvar org-babel-header-arg-names:clojure '(package)) |
86fbb8ca | 50 | |
afe98dfa | 51 | (defun org-babel-expand-body:clojure (body params) |
86fbb8ca | 52 | "Expand BODY according to PARAMS, return the expanded body." |
acedf35c CD |
53 | (let* ((vars (mapcar #'cdr (org-babel-get-header params :var))) |
54 | (result-params (cdr (assoc :result-params params))) | |
55 | (print-level nil) (print-length nil) | |
56 | (body (org-babel-trim | |
57 | (if (> (length vars) 0) | |
58 | (concat "(let [" | |
59 | (mapconcat | |
60 | (lambda (var) | |
61 | (format "%S (quote %S)" (car var) (cdr var))) | |
62 | vars "\n ") | |
63 | "]\n" body ")") | |
64 | body)))) | |
3ab2c837 BG |
65 | (cond ((or (member "code" result-params) (member "pp" result-params)) |
66 | (format (concat "(let [org-mode-print-catcher (java.io.StringWriter.)] " | |
67 | "(clojure.pprint/with-pprint-dispatch clojure.pprint/%s-dispatch " | |
68 | "(clojure.pprint/pprint (do %s) org-mode-print-catcher) " | |
69 | "(str org-mode-print-catcher)))") | |
70 | (if (member "code" result-params) "code" "simple") body)) | |
71 | ;; if (:results output), collect printed output | |
72 | ((member "output" result-params) | |
73 | (format "(clojure.core/with-out-str %s)" body)) | |
74 | (t body)))) | |
86fbb8ca CD |
75 | |
76 | (defun org-babel-execute:clojure (body params) | |
acedf35c | 77 | "Execute a block of Clojure code with Babel." |
86fbb8ca | 78 | (require 'slime) (require 'swank-clojure) |
acedf35c CD |
79 | (with-temp-buffer |
80 | (insert (org-babel-expand-body:clojure body params)) | |
3ab2c837 BG |
81 | ((lambda (result) |
82 | (let ((result-params (cdr (assoc :result-params params)))) | |
83 | (if (or (member "scalar" result-params) | |
84 | (member "verbatim" result-params)) | |
85 | result | |
86 | (condition-case nil (org-babel-script-escape result) | |
87 | (error result))))) | |
acedf35c CD |
88 | (slime-eval |
89 | `(swank:interactive-eval-region | |
3ab2c837 | 90 | ,(buffer-substring-no-properties (point-min) (point-max))) |
acedf35c | 91 | (cdr (assoc :package params)))))) |
86fbb8ca CD |
92 | |
93 | (provide 'ob-clojure) | |
94 | ||
5b409b39 | 95 | |
86fbb8ca CD |
96 | |
97 | ;;; ob-clojure.el ends here |