1
0
mirror of https://git.savannah.gnu.org/git/emacs/org-mode.git synced 2024-11-23 07:18:53 +00:00
org-mode/lisp/ob-clojure.el

289 lines
10 KiB
EmacsLisp
Raw Normal View History

Activate lexical binding in some libraries * lisp/ob-C.el (org-babel-prep-session:C): (org-babel-load-session:C): * lisp/ob-J.el: (org-babel-expand-body:J): (org-babel-execute:J): * lisp/ob-R.el: (org-babel-expand-body:R): * lisp/ob-abc.el: (org-babel-execute:abc): (org-babel-prep-session:abc): * lisp/ob-asymptote.el: (org-babel-execute:asymptote): (org-babel-prep-session:asymptote): * lisp/ob-awk.el: (org-babel-expand-body:awk): * lisp/ob-calc.el: (org-babel-expand-body:calc): * lisp/ob-clojure.el: * lisp/ob-comint.el: (org-babel-comint-in-buffer): (org-babel-comint-with-output): (org-babel-comint-eval-invisibly-and-wait-for-file): * lisp/ob-coq.el: * lisp/ob-css.el: (org-babel-execute:css): (org-babel-prep-session:css): * lisp/ob-ditaa.el: (org-babel-execute:ditaa): (org-babel-prep-session:ditaa): * lisp/ob-dot.el: (org-babel-execute:dot): (org-babel-prep-session:dot): * lisp/ob-ebnf.el: * lisp/ob-emacs-lisp.el: * lisp/ob-eval.el: * lisp/ob-forth.el: * lisp/ob-fortran.el: (org-babel-execute:fortran): (org-babel-prep-session:fortran): (org-babel-load-session:fortran): * lisp/ob-gnuplot.el: (org-babel-expand-body:gnuplot): (org-babel-prep-session:gnuplot): (org-babel-gnuplot-initiate-session): * lisp/ob-groovy.el: (org-babel-prep-session:groovy): (org-babel-groovy-initiate-session): * lisp/ob-haskell.el: (org-babel-haskell-initiate-session): * lisp/ob-io.el: (org-babel-prep-session:io): (org-babel-io-initiate-session): * lisp/ob-java.el: (org-babel-execute:java): * lisp/ob-js.el: * lisp/ob-keys.el: * lisp/ob-latex.el: (org-babel-prep-session:latex): * lisp/ob-ledger.el: (org-babel-execute:ledger): (org-babel-prep-session:ledger): * lisp/ob-lilypond.el: (org-babel-lilypond-commands): (org-babel-lilypond-process-basic): (org-babel-prep-session:lilypond): (org-babel-lilypond-parse-line-num): * lisp/ob-lisp.el: * lisp/ob-makefile.el: (org-babel-execute:makefile): (org-babel-prep-session:makefile): * lisp/ob-matlab.el: * lisp/ob-maxima.el: (org-babel-prep-session:maxima): * lisp/ob-mscgen.el: (org-babel-prep-session:mscgen): * lisp/ob-ocaml.el: (org-babel-execute:ocaml): (org-babel-prep-session:ocaml): * lisp/ob-octave.el: (org-babel-execute:octave): (org-babel-octave-initiate-session): * lisp/ob-org.el: (org-babel-prep-session:org): * lisp/ob-perl.el: (org-babel-prep-session:perl): (org-babel-perl--var-to-perl): (org-babel-perl-initiate-session): * lisp/ob-picolisp.el: (org-babel-expand-body:picolisp): (org-babel-execute:picolisp): * lisp/ob-plantuml.el: (org-babel-execute:plantuml): (org-babel-prep-session:plantuml): * lisp/ob-processing.el: (org-babel-prep-session:processing): * lisp/ob-python.el: (org-babel-python-initiate-session): * lisp/ob-ref.el: (org-babel-ref-resolve): * lisp/ob-ruby.el: (org-babel-ruby-initiate-session): * lisp/ob-sass.el: (org-babel-execute:sass): (org-babel-prep-session:sass): * lisp/ob-scala.el: (org-babel-execute:scala): (org-babel-prep-session:scala): (org-babel-scala-initiate-session): * lisp/ob-scheme.el: * lisp/ob-screen.el: (org-babel-prep-session:screen): (org-babel-screen-session-write-temp-file): (org-babel-screen-test): * lisp/ob.el: * lisp/org-colview.el: (org-columns-todo): (org-columns-set-tags-or-toggle): (org-columns-new): (org-columns-uncompile-format): (org-agenda-colview-summarize): * lisp/org-footnote.el: (electric-indent-mode): * lisp/org-indent.el: (org-indent-refresh-maybe): * lisp/org-list.el: * lisp/org-macro.el: (org-macro--collect-macros): * lisp/org-src.el: * lisp/org-table.el: (sort-fold-case): (org-table-create): (org-table-field-info): (org-table-transpose-table-at-point): (org-table-remove-rectangle-highlight): (orgtbl-create-or-convert-from-region): (org-define-lookup-function): * lisp/ox-ascii.el: (org-ascii-format-drawer-function): (org-ascii--has-caption-p): (org-ascii-bold): (org-ascii-center-block): (org-ascii-clock): (org-ascii-code): (org-ascii-dynamic-block): (org-ascii-entity): (org-ascii-example-block): (org-ascii-export-snippet): (org-ascii-export-block): (org-ascii-fixed-width): (org-ascii-footnote-reference): (org-ascii-horizontal-rule): (org-ascii-inline-src-block): (org-ascii-format-inlinetask-default): (org-ascii-italic): (org-ascii-keyword): (org-ascii-latex-environment): (org-ascii-latex-fragment): (org-ascii-line-break): (org-ascii-node-property): (org-ascii-planning): (org-ascii-quote-block): (org-ascii-radio-target): (org-ascii-special-block): (org-ascii-src-block): (org-ascii-statistics-cookie): (org-ascii-subscript): (org-ascii-superscript): (org-ascii-strike-through): (org-ascii-timestamp): (org-ascii-underline): (org-ascii-verbatim): (org-ascii-verse-block): (org-ascii-filter-headline-blank-lines): (org-ascii-filter-paragraph-spacing): (org-ascii-filter-comment-spacing): Use lexical binding.
2015-10-26 00:56:00 +00:00
;;; ob-clojure.el --- Babel Functions for Clojure -*- lexical-binding: t; -*-
2020-01-01 18:38:46 +00:00
;; Copyright (C) 2009-2020 Free Software Foundation, Inc.
;; Author: Joel Boehland, Eric Schulte, Oleh Krehel, Frederick Giasson
;;
;; Keywords: literate programming, reproducible research
;; Homepage: https://orgmode.org
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; 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,
;; 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.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Support for evaluating clojure code
;; Requirements:
;; - clojure (at least 1.2.0)
;; - clojure-mode
;; - inf-clojure, cider or SLIME
;; 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
;; 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
;;; Code:
(require 'cl-lib)
(require 'ob)
(require 'org-macs)
(declare-function cider-jack-in "ext:cider" (&optional prompt-project cljs-too))
2015-10-11 21:56:35 +00:00
(declare-function cider-current-connection "ext:cider-client" (&optional type))
(declare-function cider-current-ns "ext:cider-client" ())
(declare-function cider-repls "ext:cider-connection" (&optional type ensure))
(declare-function nrepl--merge "ext:nrepl-client" (dict1 dict2))
2014-10-13 20:05:12 +00:00
(declare-function nrepl-dict-get "ext:nrepl-client" (dict key))
(declare-function nrepl-dict-put "ext:nrepl-client" (dict key value))
(declare-function nrepl-request:eval "ext:nrepl-client" (input callback connection &optional ns line column additional-params tooling))
(declare-function nrepl-sync-request:eval "ext:nrepl-client" (input connection &optional ns tooling))
(declare-function slime-eval "ext:slime" (sexp &optional package))
(declare-function inf-clojure "ext:inf-clojure" (cmd))
(declare-function inf-clojure-eval-string "ext:inf-clojure" (code))
(defvar nrepl-sync-request-timeout)
(defvar cider-buffer-ns)
(defvar sesman-system)
(defvar cider-version)
(defvar org-babel-tangle-lang-exts)
(add-to-list 'org-babel-tangle-lang-exts '("clojure" . "clj"))
(add-to-list 'org-babel-tangle-lang-exts '("clojurescript" . "cljs"))
(defvar org-babel-default-header-args:clojure '())
(defvar org-babel-header-args:clojure '((ns . :any)
(package . :any)))
(defvar org-babel-default-header-args:clojurescript '())
(defvar org-babel-header-args:clojurescript '((package . :any)))
(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)))
(defcustom org-babel-clojure-sync-nrepl-timeout 10
"Timeout value, in seconds, of a Clojure sync call.
If the value is nil, timeout is disabled."
:group 'org-babel
:type 'integer
:version "26.1"
:package-version '(Org . "9.1")
:safe #'wholenump)
(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."
:type 'string
:group 'org-babel)
(defun org-babel-clojure-cider-current-ns ()
"Like `cider-current-ns' except `cider-find-ns'."
(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))
removing obsoleted optional third argument from org-babel-expand-body:LANG * lisp/ob-C.el (org-babel-expand-body:c++): removing obsoleted optional third argument (org-babel-expand-body:c++): removing obsoleted optional third argument (org-babel-C-expand): removing obsoleted optional third argument * lisp/ob-R.el: (org-babel-expand-body:R): removing obsoleted optional third argument (org-babel-execute:R): removing obsoleted optional third argument (org-babel-R-variable-assignments): removing obsoleted optional third argument * lisp/ob-asymptote.el: (org-babel-expand-body:asymptote): removing obsoleted optional third argument (org-babel-execute:asymptote): removing obsoleted optional third argument * lisp/ob-clojure.el: (org-babel-expand-body:clojure): removing obsoleted optional third argument (org-babel-execute:clojure): removing obsoleted optional third argument * lisp/ob-css.el: (org-babel-expand-body:css): removing obsoleted optional third argument * lisp/ob-ditaa.el: (org-babel-expand-body:ditaa): removing obsoleted optional third argument * lisp/ob-dot.el: (org-babel-expand-body:dot): removing obsoleted optional third argument (org-babel-execute:dot): removing obsoleted optional third argument * lisp/ob-emacs-lisp.el: (org-babel-expand-body:emacs-lisp): removing obsoleted optional third argument (org-babel-execute:emacs-lisp): removing obsoleted optional third argument * lisp/ob-gnuplot.el: (org-babel-expand-body:gnuplot): removing obsoleted optional third argument * lisp/ob-haskell.el: (org-babel-expand-body:haskell): removing obsoleted optional third argument (org-babel-execute:haskell): removing obsoleted optional third argument (org-babel-load-session:haskell): removing obsoleted optional third (org-babel-prep-session:haskell): removing obsoleted optional third * lisp/ob-js.el: (org-babel-expand-body:js): removing obsoleted optional third argument (org-babel-execute:js): removing obsoleted optional third argument * lisp/ob-latex.el: (org-babel-expand-body:latex): removing obsoleted optional third argument * lisp/ob-lisp.el: (org-babel-expand-body:lisp): removing obsoleted optional third argument (org-babel-execute:lisp): removing obsoleted optional third argument * lisp/ob-mscgen.el: (org-babel-expand-body:mscgen): removing obsoleted optional third argument * lisp/ob-ocaml.el: (org-babel-expand-body:ocaml): removing obsoleted optional third argument (org-babel-execute:ocaml): removing obsoleted optional third argument * lisp/ob-octave.el: (org-babel-expand-body:matlab): removing obsoleted optional third argument (org-babel-expand-body:octave): removing obsoleted optional third argument (org-babel-execute:octave): removing obsoleted optional third argument (org-babel-octave-variable-assignments): removing obsoleted optional third * lisp/ob-org.el: (org-babel-expand-body:org): removing obsoleted optional third argument * lisp/ob-perl.el: (org-babel-expand-body:perl): removing obsoleted optional third argument (org-babel-execute:perl): removing obsoleted optional third argument * lisp/ob-plantuml.el: (org-babel-expand-body:plantuml): removing obsoleted optional third argument * lisp/ob-python.el: (org-babel-expand-body:python): removing obsoleted optional third argument (org-babel-execute:python): removing obsoleted optional third argument (org-babel-python-variable-assignments): removing obsoleted optional third * lisp/ob-ruby.el: (org-babel-expand-body:ruby): removing obsoleted optional third argument (org-babel-execute:ruby): removing obsoleted optional third argument * lisp/ob-sass.el: (org-babel-expand-body:sass): removing obsoleted optional third argument * lisp/ob-scheme.el: (org-babel-expand-body:scheme): removing obsoleted optional third argument (org-babel-execute:scheme): removing obsoleted optional third argument * lisp/ob-screen.el: (org-babel-expand-body:screen): removing obsoleted optional third argument * lisp/ob-sh.el: (org-babel-expand-body:sh): removing obsoleted optional third argument (org-babel-execute:sh): removing obsoleted optional third argument (org-babel-sh-variable-assignments): removing obsoleted optional third * lisp/ob-sql.el: (org-babel-expand-body:sql): removing obsoleted optional third argument * lisp/ob-sqlite.el: (org-babel-expand-body:sqlite): removing obsoleted optional third argument (org-babel-execute:sqlite): removing obsoleted optional third argument * lisp/ob.el: (org-babel-expand-body:generic): removing obsoleted optional third argument
2010-10-16 03:35:45 +00:00
(defun org-babel-expand-body:clojure (body params)
"Expand BODY according to PARAMS, return the expanded body."
babel: small change in API. * lisp/ob-core.el (org-babel--get-vars): New function. (org-babel-get-header): Delete. * lisp/ob-C.el (org-babel-C-expand-C): (org-babel-C-expand-D): * lisp/ob-R.el (org-babel-variable-assignments:R): * lisp/ob-abc.el (org-babel-expand-body:abc): * lisp/ob-asymptote.el (org-babel-variable-assignments:asymptote): * lisp/ob-awk.el (org-babel-execute:awk): * lisp/ob-calc.el (org-babel-execute:calc): * lisp/ob-clojure.el (org-babel-expand-body:clojure): * lisp/ob-dot.el (org-babel-expand-body:dot): * lisp/ob-emacs-lisp.el (org-babel-expand-body:emacs-lisp): * lisp/ob-fortran.el (org-babel-expand-body:fortran): (org-babel-fortran-ensure-main-wrap): * lisp/ob-gnuplot.el (org-babel-gnuplot-process-vars): * lisp/ob-haskell.el (org-babel-variable-assignments:haskell): * lisp/ob-js.el (org-babel-variable-assignments:js): * lisp/ob-latex.el (org-babel-expand-body:latex): * lisp/ob-lilypond.el (org-babel-expand-body:lilypond): * lisp/ob-lisp.el (org-babel-expand-body:lisp): * lisp/ob-maxima.el (org-babel-maxima-expand): * lisp/ob-ocaml.el (org-babel-variable-assignments:ocaml): * lisp/ob-octave.el (org-babel-variable-assignments:octave): * lisp/ob-org.el (org-babel-expand-body:org): * lisp/ob-perl.el (org-babel-variable-assignments:perl): * lisp/ob-picolisp.el (org-babel-expand-body:picolisp): * lisp/ob-processing.el (org-babel-variable-assignments:processing): * lisp/ob-python.el (org-babel-variable-assignments:python): * lisp/ob-ruby.el (org-babel-variable-assignments:ruby): * lisp/ob-scheme.el (org-babel-expand-body:scheme): * lisp/ob-shell.el (org-babel-variable-assignments:shell): * lisp/ob-shen.el (org-babel-expand-body:shen): * lisp/ob-sql.el (org-babel-expand-body:sql): * lisp/ob-sqlite.el (org-babel-expand-body:sqlite): Adapt to change. * etc/ORG-NEWS: Document change.
2015-10-29 19:26:11 +00:00
(let* ((vars (org-babel--get-vars params))
(ns (or (cdr (assq :ns params))
(org-babel-clojure-cider-current-ns)))
Use assq instead of assoc for :keywords * lisp/ob-C.el (org-babel-C-execute): * lisp/ob-J.el (org-babel-execute:J): * lisp/ob-R.el (org-babel-edit-prep:R): (org-babel-expand-body:R): (org-babel-execute:R): (org-babel-variable-assignments:R): (org-babel-R-initiate-session): * lisp/ob-abc.el (org-babel-execute:abc): * lisp/ob-asymptote.el (org-babel-execute:asymptote): * lisp/ob-awk.el (org-babel-execute:awk): * lisp/ob-clojure.el (org-babel-expand-body:clojure): (org-babel-execute:clojure): * lisp/ob-core.el (org-babel-expand-body:generic): (org-babel-load-in-session): (org-babel-initiate-session): (org-babel-open-src-block-result): (org-babel-process-params): (org-babel-insert-result): (org-babel-expand-noweb-references): * lisp/ob-ditaa.el (org-babel-execute:ditaa): * lisp/ob-dot.el (org-babel-execute:dot): * lisp/ob-ebnf.el (org-babel-execute:ebnf): * lisp/ob-emacs-lisp.el (org-babel-expand-body:emacs-lisp): (org-babel-execute:emacs-lisp): * lisp/ob-exp.el (org-babel-exp-process-buffer): (org-babel-exp-do-export): (org-babel-exp-code): * lisp/ob-forth.el (org-babel-execute:forth): * lisp/ob-fortran.el (org-babel-execute:fortran): * lisp/ob-gnuplot.el (org-babel-gnuplot-process-vars): (org-babel-expand-body:gnuplot): (org-babel-execute:gnuplot): * lisp/ob-groovy.el (org-babel-execute:groovy): * lisp/ob-haskell.el (org-babel-execute:haskell): * lisp/ob-io.el (org-babel-execute:io): * lisp/ob-java.el (org-babel-execute:java): * lisp/ob-js.el (org-babel-execute:js): * lisp/ob-latex.el (org-babel-execute:latex): * lisp/ob-ledger.el (org-babel-execute:ledger): * lisp/ob-lilypond.el (org-babel-lilypond-process-basic): * lisp/ob-lisp.el (org-babel-expand-body:lisp): (org-babel-execute:lisp): * lisp/ob-lua.el (org-babel-execute:lua): * lisp/ob-maxima.el (org-babel-execute:maxima): * lisp/ob-mscgen.el (org-babel-execute:mscgen): * lisp/ob-ocaml.el (org-babel-execute:ocaml): * lisp/ob-octave.el (org-babel-execute:octave): * lisp/ob-org.el (org-babel-execute:org): * lisp/ob-perl.el (org-babel-execute:perl): * lisp/ob-picolisp.el (org-babel-execute:picolisp): * lisp/ob-plantuml.el (org-babel-execute:plantuml): * lisp/ob-python.el (org-babel-execute:python): * lisp/ob-ruby.el (org-babel-execute:ruby): * lisp/ob-sass.el (org-babel-execute:sass): * lisp/ob-scala.el (org-babel-execute:scala): * lisp/ob-scheme.el (org-babel-execute:scheme): * lisp/ob-screen.el (org-babel-execute:screen): (org-babel-prep-session:screen): * lisp/ob-shell.el (org-babel-execute:shell): (org-babel-variable-assignments:shell): (org-babel-sh-evaluate): * lisp/ob-shen.el (org-babel-execute:shen): * lisp/ob-sql.el (org-babel-execute:sql): * lisp/ob-sqlite.el (org-babel-execute:sqlite): * lisp/ob-tangle.el (org-babel-tangle): (org-babel-tangle-single-block): * lisp/org-src.el (org-src-associate-babel-session): * lisp/org.el (org-next-link): Use `assq' instead of `assoc' with :keyword keys.
2016-09-22 17:45:15 +00:00
(result-params (cdr (assq :result-params params)))
(print-level nil)
(print-length nil)
;; Remove comments, they break (let [...] ...) bindings
(body (replace-regexp-in-string "^[ ]*;+.*$" "" body))
(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)
(format "%S %S" (car var) (cdr var)))
vars
"\n ")
body))))))
(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
(defvar ob-clojure-inf-clojure-tmp-output nil)
(defun ob-clojure-inf-clojure-output (s)
"Store a trimmed version of S in a variable and return S."
(sit-for .1) ; necessary to get the output ordered
(let ((s0 (org-trim s)))
(push s0 ob-clojure-inf-clojure-tmp-output))
s)
(defvar inf-clojure-buffer)
(defvar inf-clojure-comint-prompt-regexp)
(defun org-babel-execute:clojure (body params)
"Execute a block of Clojure code with Babel."
(unless org-babel-clojure-backend
(user-error "You need to customize org-babel-clojure-backend"))
(let* ((expanded (org-babel-expand-body:clojure body params))
(response (list 'dict))
(result-params (cdr (assq :result-params params)))
result)
(cond
((eq org-babel-clojure-backend 'inf-clojure)
(condition-case nil (require 'inf-clojure)
(user-error "inf-clojure not available"))
(add-hook 'comint-preoutput-filter-functions
#'ob-clojure-inf-clojure-output)
(setq ob-clojure-inf-clojure-tmp-output nil)
(unless (and inf-clojure-buffer
(buffer-live-p (get-buffer inf-clojure-buffer)))
(setq comint-prompt-regexp inf-clojure-comint-prompt-regexp)
(save-window-excursion (call-interactively #'inf-clojure))
(sit-for 5)
(setq ob-clojure-inf-clojure-tmp-output nil))
(inf-clojure-eval-string expanded)
(sit-for .3)
(remove-hook 'comint-preoutput-filter-functions
#'ob-clojure-inf-clojure-output)
(setq result
(mapconcat
(lambda (s)
;; FIXME: don't hardcode "user=>" below
(unless (or (string-match-p "user=>" s)) s))
(delete "" ob-clojure-inf-clojure-tmp-output)
"\n")))
((eq org-babel-clojure-backend 'cider)
(condition-case nil (require 'cider)
(user-error "cider not available"))
(let ((connection (cider-current-connection (cdr (assq :target params)))))
(unless connection
(message "Starting cider default session")
(org-babel-clojure-initiate-session "default"))
(setq response
(let ((nrepl-sync-request-timeout
org-babel-clojure-sync-nrepl-timeout))
(nrepl-sync-request:eval expanded connection)))
(setq result
(concat
(nrepl-dict-get response
(if (or (member "output" result-params)
(member "pp" result-params))
"out"
"value"))
(nrepl-dict-get response "ex")
(nrepl-dict-get response "root-ex")
(nrepl-dict-get response "err")))))
((eq org-babel-clojure-backend 'slime)
(require 'slime)
(with-temp-buffer
(insert expanded)
(setq result
(slime-eval
`(swank:eval-and-grab-output
,(buffer-substring-no-properties (point-min) (point-max)))
(cdr (assq :package params)))))))
(org-babel-result-cond result-params
result
(condition-case nil (org-babel-script-escape result)
(error result)))))
(defun org-babel-execute:clojurescript (body params)
"Execute a block of ClojureScript code with Babel."
(org-babel-execute:clojure body (cons '(:target . "cljs") params)))
(defun org-babel-edit-prep:clojure (babel-info)
"Set `org-edit-special' src block by injecting `cider-buffer-ns' as namespace."
(let ((namespace (cdr (assq :ns (nth 2 babel-info)))))
(when (and (eq org-babel-clojure-backend 'cider) namespace)
(setq-local cider-buffer-ns namespace))))
(defun org-babel-clojure-initiate-session (session &optional _params)
"Initiate a session named SESSION according to PARAMS."
(when (not (string= session "none"))
(save-window-excursion
(cond
;; Session is already initiated
((org-babel-comint-buffer-livep session) nil)
;; Initiate a session with CIDER jack-in
((eq org-babel-clojure-backend 'cider)
(condition-case nil (require 'cider)
(user-error "cider not available"))
(let ((session-buffer
(if (version< cider-version "0.18.0")
;; Older CIDER (without sesman) still need to use
;; old way.
(cider-jack-in nil) ; jack-in without project
;; New CIDER (with sesman to manage sessions).
(unless (cider-repls)
(let ((sesman-system 'CIDER))
(call-interactively 'sesman-link-with-directory)))
(current-buffer))))
(when (org-babel-comint-buffer-livep session-buffer)
(sit-for .3)
session-buffer)))
((eq org-babel-clojure-backend 'inf-clojure)
(user-error "Session evaluation with inf-clojure is not supported"))
((eq org-babel-clojure-backend 'slime)
(user-error "Session evaluation with SLIME is not supported"))
(t (user-error "Session initiate failed"))))))
(defun org-babel-prep-session:clojure (session params)
"Prepare SESSION according to the header arguments specified in PARAMS."
(let ((session (org-babel-clojure-initiate-session session))
(var-lines (org-babel-variable-assignments:clojure params)))
(when session
(org-babel-comint-in-buffer session
(dolist (var var-lines)
(insert var)
(comint-send-input nil t)
(org-babel-comint-wait-for-output session)
(sit-for .1)
(goto-char (point-max)))))
session))
(defun org-babel-clojure-var-to-clojure (var)
"Convert src block's VAR to Clojure variable."
(cond
((listp var)
(replace-regexp-in-string "(" "'(" var))
((stringp var)
;; Wrap Babel passed-in header argument value with quotes in Clojure.
(format "\"%s\"" var))
(t
(format "%S" var))))
(defun org-babel-variable-assignments:clojure (params)
"Return a list of Clojure statements assigning the block's variables in PARAMS."
(mapcar
(lambda (pair)
(format "(def %s %s)"
(car pair)
(org-babel-clojure-var-to-clojure (cdr pair))))
(org-babel--get-vars params)))
(provide 'ob-clojure)
;;; ob-clojure.el ends here