Improve `org-fix-tags-on-the-fly'
[org-mode/org-tableheadings.git] / lisp / ob-clojure.el
blob93674b552db8a06dcbd3a1d921f3d551c32fc705
1 ;;; ob-clojure.el --- Babel Functions for Clojure -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2018 Free Software Foundation, Inc.
5 ;; Author: Joel Boehland, Eric Schulte, Oleh Krehel, Frederick Giasson
6 ;;
7 ;; Keywords: literate programming, reproducible research
8 ;; Homepage: https://orgmode.org
10 ;; This file is part of GNU Emacs.
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.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Support for evaluating clojure code
29 ;; Requirements:
31 ;; - clojure (at least 1.2.0)
32 ;; - clojure-mode
33 ;; - either cider or SLIME
35 ;; For Cider, see https://github.com/clojure-emacs/cider
37 ;; For SLIME, the best way to install these components is by following
38 ;; the directions as set out by Phil Hagelberg (Technomancy) on the
39 ;; web page: http://technomancy.us/126
41 ;;; Code:
42 (require 'cl-lib)
43 (require 'ob)
45 (declare-function cider-current-connection "ext:cider-client" (&optional type))
46 (declare-function cider-current-ns "ext:cider-client" ())
47 (declare-function nrepl--merge "ext:nrepl-client" (dict1 dict2))
48 (declare-function nrepl-dict-get "ext:nrepl-client" (dict key))
49 (declare-function nrepl-dict-put "ext:nrepl-client" (dict key value))
50 (declare-function nrepl-request:eval "ext:nrepl-client" (input callback connection &optional ns line column additional-params tooling))
51 (declare-function nrepl-sync-request:eval "ext:nrepl-client" (input connection &optional ns tooling))
52 (declare-function org-trim "org" (s &optional keep-lead))
53 (declare-function slime-eval "ext:slime" (sexp &optional package))
55 (defvar nrepl-sync-request-timeout)
56 (defvar cider-buffer-ns)
58 (defvar org-babel-tangle-lang-exts)
59 (add-to-list 'org-babel-tangle-lang-exts '("clojure" . "clj"))
61 (defvar org-babel-default-header-args:clojure '())
62 (defvar org-babel-header-args:clojure '((package . :any)))
64 (defcustom org-babel-clojure-sync-nrepl-timeout 10
65 "Timeout value, in seconds, of a Clojure sync call.
66 If the value is nil, timeout is disabled."
67 :group 'org-babel
68 :type 'integer
69 :version "26.1"
70 :package-version '(Org . "9.1")
71 :safe #'wholenump)
73 (defcustom org-babel-clojure-backend
74 (cond ((featurep 'cider) 'cider)
75 (t 'slime))
76 "Backend used to evaluate Clojure code blocks."
77 :group 'org-babel
78 :type '(choice
79 (const :tag "cider" cider)
80 (const :tag "SLIME" slime)))
82 (defcustom org-babel-clojure-default-ns "user"
83 "Default Clojure namespace for src block when all find ns ways failed."
84 :type 'string
85 :group 'org-babel)
87 (defun org-babel-clojure-cider-current-ns ()
88 "Like `cider-current-ns' except `cider-find-ns'."
89 (or cider-buffer-ns
90 (let ((repl-buf (cider-current-connection)))
91 (and repl-buf (buffer-local-value 'cider-buffer-ns repl-buf)))
92 org-babel-clojure-default-ns))
94 (defun org-babel-expand-body:clojure (body params)
95 "Expand BODY according to PARAMS, return the expanded body."
96 (let* ((vars (org-babel--get-vars params))
97 (ns (or (cdr (assq :ns params))
98 (org-babel-clojure-cider-current-ns)))
99 (result-params (cdr (assq :result-params params)))
100 (print-level nil)
101 (print-length nil)
102 (body
103 (org-trim
104 (format "(ns %s)\n%s"
105 ;; Source block specified namespace :ns.
107 ;; Variables binding.
108 (if (null vars) (org-trim body)
109 (format "(let [%s]\n%s)"
110 (mapconcat
111 (lambda (var)
112 (format "%S (quote %S)" (car var) (cdr var)))
113 vars
114 "\n ")
115 body))))))
116 (if (or (member "code" result-params)
117 (member "pp" result-params))
118 (format "(clojure.pprint/pprint (do %s))" body)
119 body)))
121 (defun org-babel-execute:clojure (body params)
122 "Execute a block of Clojure code with Babel.
123 The underlying process performed by the code block can be output
124 using the :show-process parameter."
125 (let* ((expanded (org-babel-expand-body:clojure body params))
126 (response (list 'dict))
127 result)
128 (cl-case org-babel-clojure-backend
129 (cider
130 (require 'cider)
131 (let ((result-params (cdr (assq :result-params params)))
132 (show (cdr (assq :show-process params))))
133 (if (member show '(nil "no"))
134 ;; Run code without showing the process.
135 (progn
136 (setq response
137 (let ((nrepl-sync-request-timeout
138 org-babel-clojure-sync-nrepl-timeout))
139 (nrepl-sync-request:eval expanded
140 (cider-current-connection))))
141 (setq result
142 (concat
143 (nrepl-dict-get response
144 (if (or (member "output" result-params)
145 (member "pp" result-params))
146 "out"
147 "value"))
148 (nrepl-dict-get response "ex")
149 (nrepl-dict-get response "root-ex")
150 (nrepl-dict-get response "err"))))
151 ;; Show the process in an output buffer/window.
152 (let ((process-buffer (switch-to-buffer-other-window
153 "*Clojure Show Process Sub Buffer*"))
154 status)
155 ;; Run the Clojure code in nREPL.
156 (nrepl-request:eval
157 expanded
158 (lambda (resp)
159 (when (member "out" resp)
160 ;; Print the output of the nREPL in the output buffer.
161 (princ (nrepl-dict-get resp "out") process-buffer))
162 (when (member "ex" resp)
163 ;; In case there is an exception, then add it to the
164 ;; output buffer as well.
165 (princ (nrepl-dict-get resp "ex") process-buffer)
166 (princ (nrepl-dict-get resp "root-ex") process-buffer))
167 (when (member "err" resp)
168 ;; In case there is an error, then add it to the
169 ;; output buffer as well.
170 (princ (nrepl-dict-get resp "err") process-buffer))
171 (nrepl--merge response resp)
172 ;; Update the status of the nREPL output session.
173 (setq status (nrepl-dict-get response "status")))
174 (cider-current-connection))
176 ;; Wait until the nREPL code finished to be processed.
177 (while (not (member "done" status))
178 (nrepl-dict-put response "status" (remove "need-input" status))
179 (accept-process-output nil 0.01)
180 (redisplay))
182 ;; Delete the show buffer & window when the processing is
183 ;; finalized.
184 (mapc #'delete-window
185 (get-buffer-window-list process-buffer nil t))
186 (kill-buffer process-buffer)
188 ;; Put the output or the value in the result section of
189 ;; the code block.
190 (setq result
191 (concat
192 (nrepl-dict-get response
193 (if (or (member "output" result-params)
194 (member "pp" result-params))
195 "out"
196 "value"))
197 (nrepl-dict-get response "ex")
198 (nrepl-dict-get response "root-ex")
199 (nrepl-dict-get response "err")))))))
200 (slime
201 (require 'slime)
202 (with-temp-buffer
203 (insert expanded)
204 (setq result
205 (slime-eval
206 `(swank:eval-and-grab-output
207 ,(buffer-substring-no-properties (point-min) (point-max)))
208 (cdr (assq :package params)))))))
209 (org-babel-result-cond (cdr (assq :result-params params))
210 result
211 (condition-case nil (org-babel-script-escape result)
212 (error result)))))
214 (provide 'ob-clojure)
216 ;;; ob-clojure.el ends here