2015-10-25 20:56:00 -04:00
|
|
|
;;; ob-clojure.el --- Babel Functions for Clojure -*- lexical-binding: t; -*-
|
2009-11-09 16:17:34 -05:00
|
|
|
|
2021-01-01 14:55:31 -05:00
|
|
|
;; Copyright (C) 2009-2021 Free Software Foundation, Inc.
|
2009-11-09 16:17:34 -05:00
|
|
|
|
2016-11-18 12:01:02 -05:00
|
|
|
;; Author: Joel Boehland, Eric Schulte, Oleh Krehel, Frederick Giasson
|
2020-10-26 03:55:32 -04:00
|
|
|
;; Maintainer: Bastien Guerry <bzg@gnu.org>
|
2013-10-03 12:23:16 -04:00
|
|
|
;;
|
2009-11-09 16:17:34 -05:00
|
|
|
;; Keywords: literate programming, reproducible research
|
2018-01-16 11:22:00 -05:00
|
|
|
;; Homepage: https://orgmode.org
|
2009-11-09 16:17:34 -05:00
|
|
|
|
2010-06-25 12:32:35 -04:00
|
|
|
;; This file is part of GNU Emacs.
|
2009-11-09 16:17:34 -05:00
|
|
|
|
2010-06-25 12:32:35 -04:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2009-11-09 16:17:34 -05:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2010-06-25 12:32:35 -04: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 16:17:34 -05: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 12:32:35 -04:00
|
|
|
|
2009-11-09 16:17:34 -05:00
|
|
|
;; You should have received a copy of the GNU General Public License
|
2017-09-13 18:52:52 -04:00
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
2009-11-09 16:17:34 -05:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
2014-10-06 05:56:04 -04:00
|
|
|
;; Support for evaluating clojure code
|
2009-11-09 16:17:34 -05:00
|
|
|
|
2013-11-04 05:30:34 -05:00
|
|
|
;; Requirements:
|
2009-11-09 16:17:34 -05:00
|
|
|
|
2013-11-04 05:30:34 -05:00
|
|
|
;; - clojure (at least 1.2.0)
|
|
|
|
;; - clojure-mode
|
2020-02-16 05:16:00 -05:00
|
|
|
;; - inf-clojure, cider or SLIME
|
2009-11-09 16:17:34 -05:00
|
|
|
|
2020-02-16 05:16:00 -05: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 12:23:16 -04:00
|
|
|
|
2013-11-04 05:30:34 -05: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 16:17:34 -05:00
|
|
|
;;; Code:
|
2010-06-11 19:02:42 -04:00
|
|
|
(require 'ob)
|
2010-07-02 22:21:31 -04:00
|
|
|
|
2015-10-11 17:56:35 -04:00
|
|
|
(declare-function cider-current-connection "ext:cider-client" (&optional type))
|
2017-03-07 03:00:27 -05:00
|
|
|
(declare-function cider-current-ns "ext:cider-client" ())
|
2020-02-16 17:43:56 -05: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 16:05:12 -04:00
|
|
|
(declare-function nrepl-dict-get "ext:nrepl-client" (dict key))
|
2018-03-25 23:35:21 -04:00
|
|
|
(declare-function nrepl-sync-request:eval "ext:nrepl-client" (input connection &optional ns tooling))
|
2020-02-16 17:43:56 -05:00
|
|
|
(declare-function sesman-start-session "ext:sesman" (system))
|
2013-11-04 05:30:34 -05:00
|
|
|
(declare-function slime-eval "ext:slime" (sexp &optional package))
|
|
|
|
|
2018-03-25 23:35:21 -04:00
|
|
|
(defvar cider-buffer-ns)
|
2016-11-18 12:01:02 -05:00
|
|
|
|
2011-06-21 18:34:41 -04:00
|
|
|
(defvar org-babel-tangle-lang-exts)
|
2010-06-17 13:21:14 -04:00
|
|
|
(add-to-list 'org-babel-tangle-lang-exts '("clojure" . "clj"))
|
2020-02-13 10:23:03 -05:00
|
|
|
(add-to-list 'org-babel-tangle-lang-exts '("clojurescript" . "cljs"))
|
2010-06-17 13:21:14 -04:00
|
|
|
|
2010-06-25 13:12:54 -04:00
|
|
|
(defvar org-babel-default-header-args:clojure '())
|
2020-02-16 17:43:56 -05:00
|
|
|
(defvar org-babel-header-args:clojure '((ns . :any) (package . :any)))
|
2020-02-13 10:23:03 -05:00
|
|
|
(defvar org-babel-default-header-args:clojurescript '())
|
|
|
|
(defvar org-babel-header-args:clojurescript '((package . :any)))
|
|
|
|
|
2020-02-16 05:16:00 -05: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-25 23:35:21 -04:00
|
|
|
(defcustom org-babel-clojure-default-ns "user"
|
2018-09-20 05:23:49 -04:00
|
|
|
"Default Clojure namespace for source block when finding ns failed."
|
2018-03-25 23:35:21 -04:00
|
|
|
:type 'string
|
|
|
|
:group 'org-babel)
|
|
|
|
|
2010-10-15 23:35:45 -04:00
|
|
|
(defun org-babel-expand-body:clojure (body params)
|
2010-06-11 20:12:15 -04:00
|
|
|
"Expand BODY according to PARAMS, return the expanded body."
|
2015-10-29 15:26:11 -04:00
|
|
|
(let* ((vars (org-babel--get-vars params))
|
2018-03-25 23:35:21 -04:00
|
|
|
(ns (or (cdr (assq :ns params))
|
2020-02-16 20:37:55 -05: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 13:45:15 -04:00
|
|
|
(result-params (cdr (assq :result-params params)))
|
2018-03-25 23:35:21 -04:00
|
|
|
(print-level nil)
|
|
|
|
(print-length nil)
|
2020-02-12 05:24:07 -05:00
|
|
|
;; Remove comments, they break (let [...] ...) bindings
|
|
|
|
(body (replace-regexp-in-string "^[ ]*;+.*$" "" body))
|
2019-01-09 20:34:39 -05: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 05:24:07 -05:00
|
|
|
(format "%S %S" (car var) (cdr var)))
|
2019-01-09 20:34:39 -05:00
|
|
|
vars
|
|
|
|
"\n ")
|
|
|
|
body))))))
|
2014-04-14 06:28:02 -04:00
|
|
|
(if (or (member "code" result-params)
|
|
|
|
(member "pp" result-params))
|
|
|
|
(format "(clojure.pprint/pprint (do %s))" body)
|
|
|
|
body)))
|
2010-04-11 16:29:24 -04:00
|
|
|
|
2020-02-16 20:37:55 -05:00
|
|
|
(defvar ob-clojure-inf-clojure-filter-out)
|
|
|
|
(defvar ob-clojure-inf-clojure-tmp-output)
|
2020-02-16 05:16:00 -05:00
|
|
|
(defun ob-clojure-inf-clojure-output (s)
|
|
|
|
"Store a trimmed version of S in a variable and return S."
|
2020-02-16 17:43:56 -05:00
|
|
|
(let ((s0 (org-trim
|
|
|
|
(replace-regexp-in-string
|
2020-02-16 20:37:55 -05:00
|
|
|
ob-clojure-inf-clojure-filter-out "" s))))
|
2020-02-16 05:16:00 -05:00
|
|
|
(push s0 ob-clojure-inf-clojure-tmp-output))
|
|
|
|
s)
|
|
|
|
|
2020-02-16 17:43:56 -05: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 05:16:00 -05:00
|
|
|
(defvar inf-clojure-buffer)
|
2020-02-16 20:37:55 -05:00
|
|
|
(defvar comint-prompt-regexp)
|
2020-02-16 05:16:00 -05:00
|
|
|
(defvar inf-clojure-comint-prompt-regexp)
|
2020-02-16 17:43:56 -05: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-16 20:37:55 -05:00
|
|
|
(setq comint-prompt-regexp inf-clojure-comint-prompt-regexp)
|
2020-02-16 17:43:56 -05:00
|
|
|
(funcall-interactively #'inf-clojure cmd)
|
|
|
|
(goto-char (point-max))))
|
2020-02-16 20:37:55 -05:00
|
|
|
(sit-for 1))
|
2020-02-16 17:43:56 -05:00
|
|
|
;; Now evaluate the code
|
2020-02-16 20:37:55 -05: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 17:43:56 -05: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-16 20:37:55 -05: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 17:43:56 -05: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 15:25:46 -04:00
|
|
|
(defun org-babel-execute:clojure (body params)
|
2020-02-16 05:16:00 -05: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-25 23:35:21 -04:00
|
|
|
(let* ((expanded (org-babel-expand-body:clojure body params))
|
2020-02-16 05:16:00 -05:00
|
|
|
(result-params (cdr (assq :result-params params)))
|
|
|
|
result)
|
2020-02-16 17:43:56 -05: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 05:16:00 -05:00
|
|
|
(org-babel-result-cond result-params
|
2014-01-30 06:10:37 -05:00
|
|
|
result
|
|
|
|
(condition-case nil (org-babel-script-escape result)
|
|
|
|
(error result)))))
|
2013-10-03 12:23:16 -04:00
|
|
|
|
2020-02-13 10:18:32 -05:00
|
|
|
(defun org-babel-execute:clojurescript (body params)
|
2020-02-16 17:43:56 -05:00
|
|
|
"Evaluate BODY with PARAMS as ClojureScript code."
|
2020-02-13 10:18:32 -05:00
|
|
|
(org-babel-execute:clojure body (cons '(:target . "cljs") params)))
|
|
|
|
|
2010-06-11 19:02:42 -04:00
|
|
|
(provide 'ob-clojure)
|
2010-06-25 12:32:35 -04:00
|
|
|
|
2010-06-11 19:02:42 -04:00
|
|
|
;;; ob-clojure.el ends here
|