2015-10-26 00:56:00 +00:00
|
|
|
;;; ob-lisp.el --- Babel Functions for Common Lisp -*- lexical-binding: t; -*-
|
2010-08-03 15:09:11 +00:00
|
|
|
|
2024-01-02 01:47:10 +00:00
|
|
|
;; Copyright (C) 2009-2024 Free Software Foundation, Inc.
|
2010-08-03 15:09:11 +00:00
|
|
|
|
2012-04-01 22:53:28 +00:00
|
|
|
;; Authors: Joel Boehland
|
|
|
|
;; Eric Schulte
|
|
|
|
;; David T. O'Toole <dto@gnu.org>
|
2011-03-31 06:11:58 +00:00
|
|
|
;; Keywords: literate programming, reproducible research
|
2021-09-26 07:44:29 +00:00
|
|
|
;; URL: https://orgmode.org
|
2010-08-03 15:09:11 +00:00
|
|
|
|
2011-03-31 06:11:58 +00:00
|
|
|
;; This file is part of GNU Emacs.
|
2010-08-03 15:09:11 +00:00
|
|
|
|
2011-03-31 06:11:58 +00:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2010-08-03 15:09:11 +00:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2011-03-31 06:11:58 +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,
|
2010-08-03 15:09:11 +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.
|
2011-03-31 06:11:58 +00:00
|
|
|
|
2010-08-03 15:09:11 +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/>.
|
2010-08-03 15:09:11 +00:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
2016-04-01 16:46:36 +00:00
|
|
|
;;; Support for evaluating Common Lisp code, relies on SLY or SLIME
|
|
|
|
;;; for all eval.
|
2010-08-03 15:09:11 +00:00
|
|
|
|
|
|
|
;;; Requirements:
|
|
|
|
|
2016-04-01 16:46:36 +00:00
|
|
|
;; Requires SLY (Sylvester the Cat's Common Lisp IDE) or SLIME
|
|
|
|
;; (Superior Lisp Interaction Mode for Emacs). See:
|
|
|
|
;; - https://github.com/capitaomorte/sly
|
2021-03-21 18:55:14 +00:00
|
|
|
;; - https://common-lisp.net/project/slime/
|
2010-08-03 15:09:11 +00:00
|
|
|
|
|
|
|
;;; Code:
|
2022-08-04 13:53:05 +00:00
|
|
|
|
|
|
|
(require 'org-macs)
|
|
|
|
(org-assert-version)
|
|
|
|
|
2010-08-03 15:09:11 +00:00
|
|
|
(require 'ob)
|
2018-05-10 00:04:12 +00:00
|
|
|
(require 'org-macs)
|
2010-11-13 00:02:24 +00:00
|
|
|
|
2016-04-01 16:46:36 +00:00
|
|
|
(declare-function sly-eval "ext:sly" (sexp &optional package))
|
2010-11-13 00:02:24 +00:00
|
|
|
(declare-function slime-eval "ext:slime" (sexp &optional package))
|
2010-08-03 15:09:11 +00:00
|
|
|
|
2011-06-21 22:34:41 +00:00
|
|
|
(defvar org-babel-tangle-lang-exts)
|
2011-03-31 06:11:58 +00:00
|
|
|
(add-to-list 'org-babel-tangle-lang-exts '("lisp" . "lisp"))
|
2010-08-03 15:09:11 +00:00
|
|
|
|
2011-03-31 06:11:58 +00:00
|
|
|
(defvar org-babel-default-header-args:lisp '())
|
2012-04-10 23:03:37 +00:00
|
|
|
(defvar org-babel-header-args:lisp '((package . :any)))
|
2010-08-03 15:09:11 +00:00
|
|
|
|
2016-04-13 21:29:41 +00:00
|
|
|
(defcustom org-babel-lisp-eval-fn #'slime-eval
|
|
|
|
"The function to be called to evaluate code on the Lisp side.
|
|
|
|
Valid values include `slime-eval' and `sly-eval'."
|
|
|
|
:group 'org-babel
|
2017-01-26 04:39:18 +00:00
|
|
|
:version "26.1"
|
2016-04-13 21:29:41 +00:00
|
|
|
:package-version '(Org . "9.0")
|
2018-03-22 18:37:45 +00:00
|
|
|
:type 'symbol)
|
2016-04-13 21:29:41 +00:00
|
|
|
|
2011-06-02 00:52:33 +00:00
|
|
|
(defcustom org-babel-lisp-dir-fmt
|
2014-08-04 01:32:14 +00:00
|
|
|
"(let ((*default-pathname-defaults* #P%S\n)) %%s\n)"
|
2011-06-02 00:52:33 +00:00
|
|
|
"Format string used to wrap code bodies to set the current directory.
|
|
|
|
For example a value of \"(progn ;; %s\\n %%s)\" would ignore the
|
2011-06-14 18:23:56 +00:00
|
|
|
current directory string."
|
|
|
|
:group 'org-babel
|
2012-03-19 20:38:12 +00:00
|
|
|
:version "24.1"
|
2011-06-14 18:23:56 +00:00
|
|
|
:type 'string)
|
2011-06-02 00:52:33 +00:00
|
|
|
|
2010-10-16 03:35:45 +00:00
|
|
|
(defun org-babel-expand-body:lisp (body params)
|
2010-08-03 15:09:11 +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))
|
2016-09-22 17:45:15 +00:00
|
|
|
(result-params (cdr (assq :result-params params)))
|
2011-03-31 06:11:58 +00:00
|
|
|
(print-level nil) (print-length nil)
|
2023-10-19 08:59:37 +00:00
|
|
|
(prologue (cdr (assq :prologue params)))
|
|
|
|
(epilogue (cdr (assq :epilogue params)))
|
2016-06-21 13:24:06 +00:00
|
|
|
(body (if (null vars) (org-trim body)
|
|
|
|
(concat "(let ("
|
|
|
|
(mapconcat
|
|
|
|
(lambda (var)
|
|
|
|
(format "(%S (quote %S))" (car var) (cdr var)))
|
|
|
|
vars "\n ")
|
2023-10-19 08:59:37 +00:00
|
|
|
")\n"
|
|
|
|
(and prologue (concat prologue "\n"))
|
|
|
|
body
|
|
|
|
(and epilogue (concat "\n" epilogue "\n"))
|
|
|
|
")"))))
|
2011-03-31 06:11:58 +00:00
|
|
|
(if (or (member "code" result-params)
|
|
|
|
(member "pp" result-params))
|
|
|
|
(format "(pprint %s)" body)
|
2010-08-03 15:09:11 +00:00
|
|
|
body)))
|
|
|
|
|
|
|
|
(defun org-babel-execute:lisp (body params)
|
2016-04-01 16:46:36 +00:00
|
|
|
"Execute a block of Common Lisp code with Babel.
|
|
|
|
BODY is the contents of the block, as a string. PARAMS is
|
|
|
|
a property list containing the parameters of the block."
|
2023-03-29 07:21:08 +00:00
|
|
|
(let (eval-and-grab-output)
|
|
|
|
(pcase org-babel-lisp-eval-fn
|
|
|
|
(`slime-eval (org-require-package 'slime "SLIME")
|
|
|
|
(setq eval-and-grab-output 'swank:eval-and-grab-output))
|
|
|
|
(`sly-eval (org-require-package 'sly "SLY")
|
|
|
|
(setq eval-and-grab-output 'slynk:eval-and-grab-output)))
|
|
|
|
(org-babel-reassemble-table
|
|
|
|
(let ((result
|
|
|
|
(funcall (if (member "output" (cdr (assq :result-params params)))
|
|
|
|
#'car #'cadr)
|
|
|
|
(with-temp-buffer
|
|
|
|
(insert (org-babel-expand-body:lisp body params))
|
|
|
|
(funcall org-babel-lisp-eval-fn
|
|
|
|
`(,eval-and-grab-output
|
|
|
|
,(let ((dir (if (assq :dir params)
|
|
|
|
(cdr (assq :dir params))
|
|
|
|
default-directory)))
|
|
|
|
(format
|
|
|
|
(if dir (format org-babel-lisp-dir-fmt dir)
|
|
|
|
"(progn %s\n)")
|
|
|
|
(buffer-substring-no-properties
|
|
|
|
(point-min) (point-max)))))
|
|
|
|
(cdr (assq :package params)))))))
|
|
|
|
(org-babel-result-cond (cdr (assq :result-params params))
|
|
|
|
(org-strip-quotes result)
|
|
|
|
(condition-case nil
|
|
|
|
(read (org-babel-lisp-vector-to-list result))
|
|
|
|
(error result))))
|
|
|
|
(org-babel-pick-name (cdr (assq :colname-names params))
|
|
|
|
(cdr (assq :colnames params)))
|
|
|
|
(org-babel-pick-name (cdr (assq :rowname-names params))
|
|
|
|
(cdr (assq :rownames params))))))
|
2010-08-03 15:09:11 +00:00
|
|
|
|
2011-11-14 17:24:02 +00:00
|
|
|
(defun org-babel-lisp-vector-to-list (results)
|
2023-09-20 10:20:17 +00:00
|
|
|
"Convert #(...) values in RESULTS string into a (...) list."
|
2011-04-07 18:31:10 +00:00
|
|
|
;; TODO: better would be to replace #(...) with [...]
|
|
|
|
(replace-regexp-in-string "#(" "(" results))
|
|
|
|
|
2010-08-03 15:09:11 +00:00
|
|
|
(provide 'ob-lisp)
|
|
|
|
|
|
|
|
;;; ob-lisp.el ends here
|