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

355 lines
14 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; -*-
;; Copyright (C) 2009-2024 Free Software Foundation, Inc.
;; Author: Joel Boehland, Eric Schulte, Oleh Krehel, Frederick Giasson
;; Maintainer: Daniel Kraus <daniel@kraus.my>
;;
;; Keywords: literate programming, reproducible research
;; URL: 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 / ClojureScript code.
;; Requirements:
2022-10-28 08:48:16 +00:00
;; - Clojure (at least 1.2.0)
;; - clojure-mode
;; - babashka, nbb, Clojure CLI tools, Cider, inf-clojure or SLIME
;; For clojure-mode, see https://github.com/clojure-emacs/clojure-mode
;; For babashka, see https://github.com/babashka/babashka
;; For nbb, see https://github.com/babashka/nbb
;; For Clojure CLI tools, see https://clojure.org/guides/deps_and_cli
;; For Cider, see https://github.com/clojure-emacs/cider
;; For inf-clojure, see https://github.com/clojure-emacs/inf-clojure
;; For SLIME, see https://slime.common-lisp.dev
2022-10-28 08:48:16 +00:00
;; For SLIME, the best way to install its components is by following
;; the directions as set out by Phil Hagelberg (Technomancy) on the
2021-03-21 18:55:14 +00:00
;; web page: https://technomancy.us/126
;;; Code:
(require 'org-macs)
(org-assert-version)
(require 'ob)
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 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))
(declare-function nrepl-sync-request:eval "ext:nrepl-client" (input connection &optional ns tooling))
(declare-function sesman-start-session "ext:sesman" (system))
(declare-function slime-eval "ext:slime" (sexp &optional package))
(defvar cider-buffer-ns)
(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)
(backend . ((inf-clojure cider slime babashka nbb)))))
(defvar org-babel-default-header-args:clojurescript '())
(defvar org-babel-header-args:clojurescript '((package . :any)))
(defcustom org-babel-clojure-backend (cond
((executable-find "bb") 'babashka)
((executable-find "clojure") 'clojure-cli)
((featurep 'cider) 'cider)
((featurep 'inf-clojure) 'inf-clojure)
((featurep 'slime) 'slime)
(t nil))
"Backend used to evaluate Clojure code blocks."
:group 'org-babel
:package-version '(Org . "9.7")
:type '(choice
(const :tag "babashka" babashka)
(const :tag "clojure-cli" clojure-cli)
(const :tag "cider" cider)
(const :tag "inf-clojure" inf-clojure)
(const :tag "slime" slime)
(const :tag "Not configured yet" nil)))
(defcustom org-babel-clojurescript-backend
(cond
((or (executable-find "nbb") (executable-find "npx")) 'nbb)
((featurep 'cider) 'cider)
(t nil))
"Backend used to evaluate ClojureScript code blocks."
:group 'org-babel
:package-version '(Org . "9.7")
:type '(choice
(const :tag "nbb" nbb)
(const :tag "cider" cider)
(const :tag "Not configured yet" nil)))
(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)
(defcustom ob-clojure-babashka-command (executable-find "bb")
"Babashka command used by the Clojure `babashka' backend."
:type '(choice file (const nil))
:group 'org-babel
:package-version '(Org . "9.6"))
(defcustom ob-clojure-nbb-command (or (executable-find "nbb")
(when-let (npx (executable-find "npx"))
(concat npx " nbb")))
"Nbb command used by the ClojureScript `nbb' backend."
:type '(choice string (const nil))
:group 'org-babel
:package-version '(Org . "9.7"))
(defcustom ob-clojure-cli-command (when-let (cmd (executable-find "clojure"))
(concat cmd " -M"))
"Clojure CLI command used by the Clojure `clojure-cli' backend."
:type '(choice string (const nil))
:group 'org-babel
:package-version '(Org . "9.7"))
(defun org-babel-expand-body:clojure (body params &optional cljs-p)
"Expand BODY according to PARAMS, return the expanded body.
When CLJS-P is non-nil, expand in a cljs context instead of clj."
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))
(backend-override (cdr (assq :backend params)))
(org-babel-clojure-backend
(cond
(backend-override (intern backend-override))
(org-babel-clojure-backend org-babel-clojure-backend)
(t (user-error "You need to customize `org-babel-clojure-backend'
or set the `:backend' header argument"))))
(ns (or (cdr (assq :ns params))
(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)))
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 the result param is set to "output" we don't have to do
;; anything special and just let the backend handle everything
(if (member "output" result-params)
body
;; If the result is not "output" (i.e. it's "value"), disable
;; stdout output and print the last returned value. Use pprint
;; instead of prn when results param is "pp" or "code".
(concat
(if (or (member "code" result-params)
(member "pp" result-params))
(concat (if cljs-p
"(require '[cljs.pprint :refer [pprint]])"
"(require '[clojure.pprint :refer [pprint]])")
" (pprint ")
"(prn ")
(if cljs-p
"(binding [cljs.core/*print-fn* (constantly nil)]"
"(binding [*out* (java.io.StringWriter.)]")
body "))"))))
2010-04-11 20:29:24 +00:00
(defvar ob-clojure-inf-clojure-filter-out)
(defvar ob-clojure-inf-clojure-tmp-output)
(defun ob-clojure-inf-clojure-output (s)
"Store a trimmed version of S in a variable and return S."
(let ((s0 (org-trim
(replace-regexp-in-string
ob-clojure-inf-clojure-filter-out "" s))))
(push s0 ob-clojure-inf-clojure-tmp-output))
s)
(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)))
(defvar inf-clojure-buffer)
(defvar comint-prompt-regexp)
(defvar inf-clojure-comint-prompt-regexp)
(defun ob-clojure-eval-with-inf-clojure (expanded params)
"Evaluate EXPANDED code block with PARAMS using inf-clojure."
Provide a uniform way to inform users about missing third-party packages * lisp/org-macs.el (org-require-package): New macro trying to load a library and displaying custom error message or warning on failure. The actual package (not library) name can be provided as optional argument. * lisp/ob-R.el (org-babel-R-initiate-session): * lisp/ob-clojure.el (ob-clojure-eval-with-inf-clojure): (ob-clojure-eval-with-cider): (ob-clojure-eval-with-slime): * lisp/ob-forth.el (org-babel-forth-session-execute): * lisp/ob-gnuplot.el (org-babel-execute:gnuplot): (org-babel-gnuplot-initiate-session): * lisp/ob-haskell.el (org-babel-interpret-haskell): (org-babel-haskell-initiate-session): * lisp/ob-js.el (org-babel-execute:js): (org-babel-js-initiate-session): * lisp/ob-julia.el (org-babel-julia-initiate-session): * lisp/ob-lisp.el (org-babel-execute:lisp): * lisp/ob-ocaml.el (org-babel-prep-session:ocaml): * lisp/ob-octave.el (org-babel-octave-initiate-session): * lisp/ob-processing.el (org-babel-processing-view-sketch): * lisp/ob-ruby.el (org-babel-execute:ruby): (org-babel-ruby-initiate-session): * lisp/ol-bbdb.el (org-bbdb-open): (org-bbdb-complete-link): (org-bbdb-anniv-export-ical): * lisp/org-agenda.el: * lisp/org-plot.el (org-plot/gnuplot): * lisp/org.el: * lisp/ox-ascii.el (org-ascii-table): * lisp/ox-html.el (org-html-htmlize-generate-css): * lisp/ox-org.el: Use the new macro. * lisp/oc-csl.el: Add FIXME. oc-csl uses a custom function doing similar job. * lisp/ob-js.el (org-babel-js-initiate-session): Add FIXME noting that the third-party package is outdated.
2023-01-23 15:06:46 +00:00
(org-require-package 'inf-clojure)
;; 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)))
(setq
org-babel-comint-prompt-regexp-old comint-prompt-regexp
comint-prompt-regexp inf-clojure-comint-prompt-regexp)
(funcall-interactively #'inf-clojure cmd)
(goto-char (point-max))))
(sit-for 1))
;; Now evaluate the code
(setq ob-clojure-inf-clojure-filter-out
(concat "^nil\\|nil$\\|\\s-*"
(or (cdr (assq :ns params))
org-babel-clojure-default-ns)
"=>\\s-*"))
(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 &optional cljs-p)
"Evaluate EXPANDED code block using cider.
When CLJS-P is non-nil, use a cljs connection instead of clj.
The PARAMS from Babel are not used in this function."
Provide a uniform way to inform users about missing third-party packages * lisp/org-macs.el (org-require-package): New macro trying to load a library and displaying custom error message or warning on failure. The actual package (not library) name can be provided as optional argument. * lisp/ob-R.el (org-babel-R-initiate-session): * lisp/ob-clojure.el (ob-clojure-eval-with-inf-clojure): (ob-clojure-eval-with-cider): (ob-clojure-eval-with-slime): * lisp/ob-forth.el (org-babel-forth-session-execute): * lisp/ob-gnuplot.el (org-babel-execute:gnuplot): (org-babel-gnuplot-initiate-session): * lisp/ob-haskell.el (org-babel-interpret-haskell): (org-babel-haskell-initiate-session): * lisp/ob-js.el (org-babel-execute:js): (org-babel-js-initiate-session): * lisp/ob-julia.el (org-babel-julia-initiate-session): * lisp/ob-lisp.el (org-babel-execute:lisp): * lisp/ob-ocaml.el (org-babel-prep-session:ocaml): * lisp/ob-octave.el (org-babel-octave-initiate-session): * lisp/ob-processing.el (org-babel-processing-view-sketch): * lisp/ob-ruby.el (org-babel-execute:ruby): (org-babel-ruby-initiate-session): * lisp/ol-bbdb.el (org-bbdb-open): (org-bbdb-complete-link): (org-bbdb-anniv-export-ical): * lisp/org-agenda.el: * lisp/org-plot.el (org-plot/gnuplot): * lisp/org.el: * lisp/ox-ascii.el (org-ascii-table): * lisp/ox-html.el (org-html-htmlize-generate-css): * lisp/ox-org.el: Use the new macro. * lisp/oc-csl.el: Add FIXME. oc-csl uses a custom function doing similar job. * lisp/ob-js.el (org-babel-js-initiate-session): Add FIXME noting that the third-party package is outdated.
2023-01-23 15:06:46 +00:00
(org-require-package 'cider "Cider")
(let ((connection (cider-current-connection (if cljs-p "cljs" "clj"))))
(unless connection (sesman-start-session 'CIDER))
(if (not connection)
;; Display in the result instead of using `user-error'
"Please reevaluate when nREPL is connected"
(let ((response (nrepl-sync-request:eval expanded connection)))
(or (nrepl-dict-get response "root-ex")
(nrepl-dict-get response "ex")
(nrepl-dict-get response "out"))))))
(defun ob-clojure-eval-with-slime (expanded params)
"Evaluate EXPANDED code block with PARAMS using slime."
Provide a uniform way to inform users about missing third-party packages * lisp/org-macs.el (org-require-package): New macro trying to load a library and displaying custom error message or warning on failure. The actual package (not library) name can be provided as optional argument. * lisp/ob-R.el (org-babel-R-initiate-session): * lisp/ob-clojure.el (ob-clojure-eval-with-inf-clojure): (ob-clojure-eval-with-cider): (ob-clojure-eval-with-slime): * lisp/ob-forth.el (org-babel-forth-session-execute): * lisp/ob-gnuplot.el (org-babel-execute:gnuplot): (org-babel-gnuplot-initiate-session): * lisp/ob-haskell.el (org-babel-interpret-haskell): (org-babel-haskell-initiate-session): * lisp/ob-js.el (org-babel-execute:js): (org-babel-js-initiate-session): * lisp/ob-julia.el (org-babel-julia-initiate-session): * lisp/ob-lisp.el (org-babel-execute:lisp): * lisp/ob-ocaml.el (org-babel-prep-session:ocaml): * lisp/ob-octave.el (org-babel-octave-initiate-session): * lisp/ob-processing.el (org-babel-processing-view-sketch): * lisp/ob-ruby.el (org-babel-execute:ruby): (org-babel-ruby-initiate-session): * lisp/ol-bbdb.el (org-bbdb-open): (org-bbdb-complete-link): (org-bbdb-anniv-export-ical): * lisp/org-agenda.el: * lisp/org-plot.el (org-plot/gnuplot): * lisp/org.el: * lisp/ox-ascii.el (org-ascii-table): * lisp/ox-html.el (org-html-htmlize-generate-css): * lisp/ox-org.el: Use the new macro. * lisp/oc-csl.el: Add FIXME. oc-csl uses a custom function doing similar job. * lisp/ob-js.el (org-babel-js-initiate-session): Add FIXME noting that the third-party package is outdated.
2023-01-23 15:06:46 +00:00
(org-require-package 'slime "SLIME")
(with-temp-buffer
(insert expanded)
(slime-eval
`(swank:eval-and-grab-output
,(buffer-substring-no-properties (point-min) (point-max)))
(cdr (assq :package params)))))
(defun ob-clojure-eval-with-cmd (cmd expanded)
"Evaluate EXPANDED code block using CMD (babashka, clojure or nbb)."
(let ((script-file (org-babel-temp-file "clojure-cmd-script-" ".clj")))
(with-temp-file script-file
(insert expanded))
(org-babel-eval
(format "%s %s" cmd (org-babel-process-file-name script-file))
"")))
(defun org-babel-execute:clojure (body params &optional cljs-p)
"Execute the BODY block of Clojure code with PARAMS using Babel.
When CLJS-P is non-nil, execute with a ClojureScript backend
instead of Clojure."
(let* ((backend-override (cdr (assq :backend params)))
(org-babel-clojure-backend
(cond
(backend-override (intern backend-override))
(org-babel-clojure-backend (if cljs-p
org-babel-clojurescript-backend
org-babel-clojure-backend))
(t (user-error "You need to customize `%S'
or set the `:backend' header argument"
(if cljs-p
org-babel-clojurescript-backend
org-babel-clojure-backend)))))
;; We allow a Clojure source block to be evaluated with the
;; nbb backend and therefore have to expand the body with
;; ClojureScript syntax when we either evaluate a
;; ClojureScript source block or use the nbb backend.
(cljs-p (or cljs-p (eq org-babel-clojure-backend 'nbb))))
(let* ((expanded (org-babel-expand-body:clojure body params cljs-p))
(result-params (cdr (assq :result-params params)))
result)
(setq result
(cond
((eq org-babel-clojure-backend 'inf-clojure)
(ob-clojure-eval-with-inf-clojure expanded params))
((eq org-babel-clojure-backend 'clojure-cli)
(ob-clojure-eval-with-cmd ob-clojure-cli-command expanded))
((eq org-babel-clojure-backend 'babashka)
(ob-clojure-eval-with-cmd ob-clojure-babashka-command expanded))
((eq org-babel-clojure-backend 'nbb)
(ob-clojure-eval-with-cmd ob-clojure-nbb-command expanded))
((eq org-babel-clojure-backend 'cider)
(ob-clojure-eval-with-cider expanded params cljs-p))
((eq org-babel-clojure-backend 'slime)
(ob-clojure-eval-with-slime expanded params))
(t (user-error "Invalid backend"))))
(org-babel-result-cond result-params
result
(condition-case nil (org-babel-script-escape result)
(error result))))))
(defun org-babel-execute:clojurescript (body params)
"Evaluate BODY with PARAMS as ClojureScript code."
(org-babel-execute:clojure body params t))
(provide 'ob-clojure)
;;; ob-clojure.el ends here