2015-10-26 00:56:00 +00:00
|
|
|
;;; ob-clojure.el --- Babel Functions for Clojure -*- lexical-binding: t; -*-
|
2009-11-09 21:17:34 +00:00
|
|
|
|
2021-01-01 19:55:31 +00:00
|
|
|
;; Copyright (C) 2009-2021 Free Software Foundation, Inc.
|
2009-11-09 21:17:34 +00:00
|
|
|
|
2016-11-18 17:01:02 +00:00
|
|
|
;; Author: Joel Boehland, Eric Schulte, Oleh Krehel, Frederick Giasson
|
2013-10-03 16:23:16 +00:00
|
|
|
;;
|
2009-11-09 21:17:34 +00:00
|
|
|
;; Keywords: literate programming, reproducible research
|
2018-01-16 16:22:00 +00:00
|
|
|
;; Homepage: https://orgmode.org
|
2009-11-09 21:17:34 +00:00
|
|
|
|
2010-06-25 16:32:35 +00:00
|
|
|
;; This file is part of GNU Emacs.
|
2009-11-09 21:17:34 +00:00
|
|
|
|
2010-06-25 16:32:35 +00:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2009-11-09 21:17:34 +00:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2010-06-25 16:32:35 +00:00
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
2009-11-09 21:17:34 +00:00
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
2010-06-25 16:32:35 +00:00
|
|
|
|
2009-11-09 21:17:34 +00:00
|
|
|
;; You should have received a copy of the GNU General Public License
|
2017-09-13 22:52:52 +00:00
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
2009-11-09 21:17:34 +00:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
2014-10-06 09:56:04 +00:00
|
|
|
;; Support for evaluating clojure code
|
2009-11-09 21:17:34 +00:00
|
|
|
|
2013-11-04 10:30:34 +00:00
|
|
|
;; Requirements:
|
2009-11-09 21:17:34 +00:00
|
|
|
|
2013-11-04 10:30:34 +00:00
|
|
|
;; - clojure (at least 1.2.0)
|
|
|
|
;; - clojure-mode
|
2020-02-16 10:16:00 +00:00
|
|
|
;; - inf-clojure, cider or SLIME
|
2009-11-09 21:17:34 +00:00
|
|
|
|
2020-02-16 10:16:00 +00:00
|
|
|
;; For clojure-mode, see https://github.com/clojure-emacs/clojure-mode
|
|
|
|
;; For cider, see https://github.com/clojure-emacs/cider
|
|
|
|
;; For inf-clojure, see https://github.com/clojure-emacs/cider
|
2013-10-03 16:23:16 +00:00
|
|
|
|
2013-11-04 10:30:34 +00:00
|
|
|
;; For SLIME, the best way to install these components is by following
|
|
|
|
;; the directions as set out by Phil Hagelberg (Technomancy) on the
|
|
|
|
;; web page: http://technomancy.us/126
|
|
|
|
|
2009-11-09 21:17:34 +00:00
|
|
|
;;; Code:
|
2010-06-11 23:02:42 +00:00
|
|
|
(require 'ob)
|
2010-07-03 02:21:31 +00:00
|
|
|
|
2015-10-11 21:56:35 +00:00
|
|
|
(declare-function cider-current-connection "ext:cider-client" (&optional type))
|
2017-03-07 08:00:27 +00:00
|
|
|
(declare-function cider-current-ns "ext:cider-client" ())
|
2020-02-16 22:43:56 +00:00
|
|
|
(declare-function inf-clojure "ext:inf-clojure" (cmd))
|
|
|
|
(declare-function inf-clojure-cmd "ext:inf-clojure" (project-type))
|
|
|
|
(declare-function inf-clojure-eval-string "ext:inf-clojure" (code))
|
|
|
|
(declare-function inf-clojure-project-type "ext:inf-clojure" ())
|
2014-10-13 20:05:12 +00:00
|
|
|
(declare-function nrepl-dict-get "ext:nrepl-client" (dict key))
|
2018-03-26 03:35:21 +00:00
|
|
|
(declare-function nrepl-sync-request:eval "ext:nrepl-client" (input connection &optional ns tooling))
|
2020-02-16 22:43:56 +00:00
|
|
|
(declare-function sesman-start-session "ext:sesman" (system))
|
2013-11-04 10:30:34 +00:00
|
|
|
(declare-function slime-eval "ext:slime" (sexp &optional package))
|
|
|
|
|
2018-03-26 03:35:21 +00:00
|
|
|
(defvar cider-buffer-ns)
|
2016-11-18 17:01:02 +00:00
|
|
|
|
2011-06-21 22:34:41 +00:00
|
|
|
(defvar org-babel-tangle-lang-exts)
|
2010-06-17 17:21:14 +00:00
|
|
|
(add-to-list 'org-babel-tangle-lang-exts '("clojure" . "clj"))
|
2020-02-13 15:23:03 +00:00
|
|
|
(add-to-list 'org-babel-tangle-lang-exts '("clojurescript" . "cljs"))
|
2010-06-17 17:21:14 +00:00
|
|
|
|
2010-06-25 17:12:54 +00:00
|
|
|
(defvar org-babel-default-header-args:clojure '())
|
2020-02-16 22:43:56 +00:00
|
|
|
(defvar org-babel-header-args:clojure '((ns . :any) (package . :any)))
|
2020-02-13 15:23:03 +00:00
|
|
|
(defvar org-babel-default-header-args:clojurescript '())
|
|
|
|
(defvar org-babel-header-args:clojurescript '((package . :any)))
|
|
|
|
|
2020-02-16 10:16:00 +00:00
|
|
|
(defcustom org-babel-clojure-backend nil
|
|
|
|
"Backend used to evaluate Clojure code blocks."
|
|
|
|
:group 'org-babel
|
|
|
|
:type '(choice
|
|
|
|
(const :tag "inf-clojure" inf-clojure)
|
|
|
|
(const :tag "cider" cider)
|
|
|
|
(const :tag "slime" slime)
|
|
|
|
(const :tag "Not configured yet" nil)))
|
|
|
|
|
2018-03-26 03:35:21 +00:00
|
|
|
(defcustom org-babel-clojure-default-ns "user"
|
2018-09-20 09:23:49 +00:00
|
|
|
"Default Clojure namespace for source block when finding ns failed."
|
2018-03-26 03:35:21 +00:00
|
|
|
:type 'string
|
|
|
|
:group 'org-babel)
|
|
|
|
|
2010-10-16 03:35:45 +00:00
|
|
|
(defun org-babel-expand-body:clojure (body params)
|
2010-06-12 00:12:15 +00:00
|
|
|
"Expand BODY according to PARAMS, return the expanded body."
|
2015-10-29 19:26:11 +00:00
|
|
|
(let* ((vars (org-babel--get-vars params))
|
2018-03-26 03:35:21 +00:00
|
|
|
(ns (or (cdr (assq :ns params))
|
2020-02-17 01:37:55 +00:00
|
|
|
(if (eq org-babel-clojure-backend 'cider)
|
|
|
|
(or cider-buffer-ns
|
|
|
|
(let ((repl-buf (cider-current-connection)))
|
|
|
|
(and repl-buf (buffer-local-value
|
|
|
|
'cider-buffer-ns repl-buf))))
|
|
|
|
org-babel-clojure-default-ns)))
|
2016-09-22 17:45:15 +00:00
|
|
|
(result-params (cdr (assq :result-params params)))
|
2018-03-26 03:35:21 +00:00
|
|
|
(print-level nil)
|
|
|
|
(print-length nil)
|
2020-02-12 10:24:07 +00:00
|
|
|
;; Remove comments, they break (let [...] ...) bindings
|
|
|
|
(body (replace-regexp-in-string "^[ ]*;+.*$" "" body))
|
2019-01-10 01:34:39 +00:00
|
|
|
(body (org-trim
|
|
|
|
(concat
|
|
|
|
;; Source block specified namespace :ns.
|
|
|
|
(and (cdr (assq :ns params)) (format "(ns %s)\n" ns))
|
|
|
|
;; Variables binding.
|
|
|
|
(if (null vars) (org-trim body)
|
|
|
|
(format "(let [%s]\n%s)"
|
|
|
|
(mapconcat
|
|
|
|
(lambda (var)
|
2020-02-12 10:24:07 +00:00
|
|
|
(format "%S %S" (car var) (cdr var)))
|
2019-01-10 01:34:39 +00:00
|
|
|
vars
|
|
|
|
"\n ")
|
|
|
|
body))))))
|
2014-04-14 10:28:02 +00:00
|
|
|
(if (or (member "code" result-params)
|
|
|
|
(member "pp" result-params))
|
|
|
|
(format "(clojure.pprint/pprint (do %s))" body)
|
|
|
|
body)))
|
2010-04-11 20:29:24 +00:00
|
|
|
|
2020-02-17 01:37:55 +00:00
|
|
|
(defvar ob-clojure-inf-clojure-filter-out)
|
|
|
|
(defvar ob-clojure-inf-clojure-tmp-output)
|
2020-02-16 10:16:00 +00:00
|
|
|
(defun ob-clojure-inf-clojure-output (s)
|
|
|
|
"Store a trimmed version of S in a variable and return S."
|
2020-02-16 22:43:56 +00:00
|
|
|
(let ((s0 (org-trim
|
|
|
|
(replace-regexp-in-string
|
2020-02-17 01:37:55 +00:00
|
|
|
ob-clojure-inf-clojure-filter-out "" s))))
|
2020-02-16 10:16:00 +00:00
|
|
|
(push s0 ob-clojure-inf-clojure-tmp-output))
|
|
|
|
s)
|
|
|
|
|
2020-02-16 22:43:56 +00:00
|
|
|
(defmacro ob-clojure-with-temp-expanded (expanded params &rest body)
|
|
|
|
"Run BODY on EXPANDED code block with PARAMS."
|
|
|
|
(declare (debug (body)) (indent 2))
|
|
|
|
`(with-temp-buffer
|
|
|
|
(insert ,expanded)
|
|
|
|
(goto-char (point-min))
|
|
|
|
(while (not (looking-at "\\s-*\\'"))
|
|
|
|
(let* ((beg (point))
|
|
|
|
(end (progn (forward-sexp) (point)))
|
|
|
|
(exp (org-babel-expand-body:clojure
|
|
|
|
(buffer-substring beg end) ,params)))
|
|
|
|
(sit-for .1)
|
|
|
|
,@body))))
|
|
|
|
|
|
|
|
(defsubst ob-clojure-string-or-list (l)
|
|
|
|
"Convert list L into a string or a list of list."
|
|
|
|
(if (and (listp l) (= (length l) 1))
|
|
|
|
(car l)
|
|
|
|
(mapcar #'list l)))
|
|
|
|
|
2020-02-16 10:16:00 +00:00
|
|
|
(defvar inf-clojure-buffer)
|
2020-02-17 01:37:55 +00:00
|
|
|
(defvar comint-prompt-regexp)
|
2020-02-16 10:16:00 +00:00
|
|
|
(defvar inf-clojure-comint-prompt-regexp)
|
2020-02-16 22:43:56 +00:00
|
|
|
(defun ob-clojure-eval-with-inf-clojure (expanded params)
|
|
|
|
"Evaluate EXPANDED code block with PARAMS using inf-clojure."
|
|
|
|
(condition-case nil (require 'inf-clojure)
|
|
|
|
(user-error "inf-clojure not available"))
|
|
|
|
;; Maybe initiate the inf-clojure session
|
|
|
|
(unless (and inf-clojure-buffer
|
|
|
|
(buffer-live-p (get-buffer inf-clojure-buffer)))
|
|
|
|
(save-window-excursion
|
|
|
|
(let* ((alias (cdr (assq :alias params)))
|
|
|
|
(cmd0 (inf-clojure-cmd (inf-clojure-project-type)))
|
|
|
|
(cmd (if alias (replace-regexp-in-string
|
|
|
|
"clojure" (format "clojure -A%s" alias)
|
|
|
|
cmd0)
|
|
|
|
cmd0)))
|
2020-02-17 01:37:55 +00:00
|
|
|
(setq comint-prompt-regexp inf-clojure-comint-prompt-regexp)
|
2020-02-16 22:43:56 +00:00
|
|
|
(funcall-interactively #'inf-clojure cmd)
|
|
|
|
(goto-char (point-max))))
|
2020-02-17 01:37:55 +00:00
|
|
|
(sit-for 1))
|
2020-02-16 22:43:56 +00:00
|
|
|
;; Now evaluate the code
|
2020-02-17 01:37:55 +00:00
|
|
|
(setq ob-clojure-inf-clojure-filter-out
|
|
|
|
(concat "^nil\\|nil$\\|\\s-*"
|
|
|
|
(or (cdr (assq :ns params))
|
|
|
|
org-babel-clojure-default-ns)
|
|
|
|
"=>\\s-*"))
|
2020-02-16 22:43:56 +00:00
|
|
|
(add-hook 'comint-preoutput-filter-functions
|
|
|
|
#'ob-clojure-inf-clojure-output)
|
|
|
|
(setq ob-clojure-inf-clojure-tmp-output nil)
|
|
|
|
(ob-clojure-with-temp-expanded expanded nil
|
|
|
|
(inf-clojure-eval-string exp))
|
|
|
|
(sit-for .5)
|
|
|
|
(remove-hook 'comint-preoutput-filter-functions
|
|
|
|
#'ob-clojure-inf-clojure-output)
|
|
|
|
;; And return the result
|
|
|
|
(ob-clojure-string-or-list
|
|
|
|
(delete nil
|
|
|
|
(mapcar
|
|
|
|
(lambda (s)
|
|
|
|
(unless (or (equal "" s)
|
|
|
|
(string-match-p "^Clojure" s))
|
|
|
|
s))
|
|
|
|
(reverse ob-clojure-inf-clojure-tmp-output)))))
|
|
|
|
|
|
|
|
(defun ob-clojure-eval-with-cider (expanded params)
|
|
|
|
"Evaluate EXPANDED code block with PARAMS using cider."
|
|
|
|
(condition-case nil (require 'cider)
|
|
|
|
(user-error "cider not available"))
|
|
|
|
(let ((connection (cider-current-connection (cdr (assq :target params))))
|
|
|
|
(result-params (cdr (assq :result-params params)))
|
|
|
|
result0)
|
|
|
|
(unless connection (sesman-start-session 'CIDER))
|
|
|
|
(if (not connection)
|
|
|
|
;; Display in the result instead of using `user-error'
|
|
|
|
(setq result0 "Please reevaluate when nREPL is connected")
|
2020-02-17 01:37:55 +00:00
|
|
|
(ob-clojure-with-temp-expanded expanded params
|
|
|
|
(let ((response (nrepl-sync-request:eval exp connection)))
|
|
|
|
(push (or (nrepl-dict-get response "root-ex")
|
|
|
|
(nrepl-dict-get response "ex")
|
|
|
|
(nrepl-dict-get
|
|
|
|
response (if (or (member "output" result-params)
|
|
|
|
(member "pp" result-params))
|
|
|
|
"out"
|
|
|
|
"value")))
|
|
|
|
result0)))
|
2020-02-16 22:43:56 +00:00
|
|
|
(ob-clojure-string-or-list
|
|
|
|
(reverse (delete "" (mapcar (lambda (r)
|
|
|
|
(replace-regexp-in-string "nil" "" r))
|
|
|
|
result0)))))))
|
|
|
|
|
|
|
|
(defun ob-clojure-eval-with-slime (expanded params)
|
|
|
|
"Evaluate EXPANDED code block with PARAMS using slime."
|
|
|
|
(condition-case nil (require 'slime)
|
|
|
|
(user-error "slime not available"))
|
|
|
|
(with-temp-buffer
|
|
|
|
(insert expanded)
|
|
|
|
(slime-eval
|
|
|
|
`(swank:eval-and-grab-output
|
|
|
|
,(buffer-substring-no-properties (point-min) (point-max)))
|
|
|
|
(cdr (assq :package params)))))
|
|
|
|
|
2013-10-03 19:25:46 +00:00
|
|
|
(defun org-babel-execute:clojure (body params)
|
2020-02-16 10:16:00 +00:00
|
|
|
"Execute a block of Clojure code with Babel."
|
|
|
|
(unless org-babel-clojure-backend
|
|
|
|
(user-error "You need to customize org-babel-clojure-backend"))
|
2018-03-26 03:35:21 +00:00
|
|
|
(let* ((expanded (org-babel-expand-body:clojure body params))
|
2020-02-16 10:16:00 +00:00
|
|
|
(result-params (cdr (assq :result-params params)))
|
|
|
|
result)
|
2020-02-16 22:43:56 +00:00
|
|
|
(setq result
|
|
|
|
(cond
|
|
|
|
((eq org-babel-clojure-backend 'inf-clojure)
|
|
|
|
(ob-clojure-eval-with-inf-clojure expanded params))
|
|
|
|
((eq org-babel-clojure-backend 'cider)
|
|
|
|
(ob-clojure-eval-with-cider expanded params))
|
|
|
|
((eq org-babel-clojure-backend 'slime)
|
|
|
|
(ob-clojure-eval-with-slime expanded params))))
|
2020-02-16 10:16:00 +00:00
|
|
|
(org-babel-result-cond result-params
|
2014-01-30 11:10:37 +00:00
|
|
|
result
|
|
|
|
(condition-case nil (org-babel-script-escape result)
|
|
|
|
(error result)))))
|
2013-10-03 16:23:16 +00:00
|
|
|
|
2020-02-13 15:18:32 +00:00
|
|
|
(defun org-babel-execute:clojurescript (body params)
|
2020-02-16 22:43:56 +00:00
|
|
|
"Evaluate BODY with PARAMS as ClojureScript code."
|
2020-02-13 15:18:32 +00:00
|
|
|
(org-babel-execute:clojure body (cons '(:target . "cljs") params)))
|
|
|
|
|
2010-06-11 23:02:42 +00:00
|
|
|
(provide 'ob-clojure)
|
2010-06-25 16:32:35 +00:00
|
|
|
|
2010-06-11 23:02:42 +00:00
|
|
|
;;; ob-clojure.el ends here
|