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
|
|
|
|
2021-01-01 19:55:31 +00:00
|
|
|
;; Copyright (C) 2009-2021 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
|
2018-01-16 16:22:00 +00:00
|
|
|
;; Homepage: 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
|
|
|
|
;; - http://common-lisp.net/project/slime/
|
2010-08-03 15:09:11 +00:00
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
(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)
|
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 ")
|
|
|
|
")\n" body ")"))))
|
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."
|
|
|
|
(require (pcase org-babel-lisp-eval-fn
|
2016-04-13 21:29:41 +00:00
|
|
|
(`slime-eval 'slime)
|
|
|
|
(`sly-eval 'sly)))
|
2011-04-07 17:31:56 +00:00
|
|
|
(org-babel-reassemble-table
|
Backport changes from Emacs revs 115081 and 115082
2013-11-12 Stefan Monnier <monnier@iro.umontreal.ca>
Address some byte-compiler warnings.
* ob-abc.el (org-babel-expand-body:abc): Use dolist.
(org-babel-execute:abc): Fix regexp quoting.
* ob-calc.el (org--var-syms): Rename from `var-syms'.
* ob-lilypond.el (ly-compile-lilyfile): Remove redundant let-binding.
* ob-table.el (sbe): Move debug declaration.
* org-clock.el (org--msg-extra): Rename from `msg-extra'.
* org.el (org-version): Avoid var name starting with _.
(org-inhibit-startup, org-called-with-limited-levels)
(org-link-search-inhibit-query, org-time-was-given)
(org-end-time-was-given, org-def, org-defdecode, org-with-time):
* org-colview.el (org-agenda-overriding-columns-format):
* org-agenda.el (org-agenda-multi, org-depend-tag-blocked)
(org-agenda-show-log-scoped):
* ob-python.el (py-which-bufname, python-shell-buffer-name):
* ob-haskell.el (org-export-copy-to-kill-ring):
* ob-exp.el (org-link-search-inhibit-query):
* ob-R.el (ess-eval-visibly-p):
* ob-core.el (org-src-window-setup): Declare before use.
(org-babel-expand-noweb-references): Remove unused `blocks-in-buffer'.
* ox-odt.el (org-odt-hfy-face-to-css):
* org-src.el (org-src-associate-babel-session, org-src-get-lang-mode):
* org-bibtex.el (org-bibtex-get, org-bibtex-ask, org-bibtex)
(org-bibtex-check):
* ob-tangle.el (org-babel-tangle, org-babel-spec-to-string)
(org-babel-tangle-single-block, org-babel-tangle-comment-links):
* ob-table.el (sbe):
* ob-sqlite.el (org-babel-sqlite-expand-vars):
* ob-sql.el (org-babel-sql-expand-vars):
* ob-shen.el (org-babel-execute:shen):
* ob-sh.el (org-babel-execute:sh, org-babel-sh-evaluate):
* ob-scala.el (org-babel-scala-evaluate):
* ob-ruby.el (org-babel-ruby-table-or-string)
(org-babel-ruby-evaluate):
* ob-python.el (org-babel-python-table-or-string)
(org-babel-python-evaluate-external-process)
(org-babel-python-evaluate-session):
* ob-picolisp.el (org-babel-execute:picolisp):
* ob-perl.el (org-babel-perl-evaluate):
* ob-maxima.el (org-babel-execute:maxima):
* ob-lisp.el (org-babel-execute:lisp):
* ob-java.el (org-babel-execute:java):
* ob-io.el (org-babel-io-evaluate):
* ob-haskell.el (org-babel-execute:haskell):
* ob-fortran.el (org-babel-execute:fortran):
* ob-exp.el (org-babel-exp-code):
* ob-emacs-lisp.el (org-babel-execute:emacs-lisp):
* ob-ditaa.el (org-babel-execute:ditaa):
* ob-core.el (org-babel-execute-src-block, org-babel-sha1-hash)
(org-babel-parse-header-arguments, org-babel-reassemble-table)
(org-babel-goto-src-block-head, org-babel-mark-block)
(org-babel-expand-noweb-references, org-babel-script-escape)
(org-babel-process-file-name):
* ob-clojure.el (org-babel-execute:clojure):
* ob-calc.el (org-babel-execute:calc):
* ob-awk.el (org-babel-execute:awk):
* ob-abc.el (org-babel-execute:abc):
* ob-R.el (org-babel-expand-body:R):
* ob-C.el (org-babel-C-execute): Avoid deprecated ((lambda) ...).
2013-11-12 Glenn Morris <rgm@gnu.org>
* ox-html.el (org-html-scripts): Add 2013 to copyright years.
(org-html-infojs-template): Copyright holder to FSF.
2013-11-12 19:57:31 +00:00
|
|
|
(let ((result
|
2016-09-22 17:45:15 +00:00
|
|
|
(funcall (if (member "output" (cdr (assq :result-params params)))
|
2016-04-01 16:46:36 +00:00
|
|
|
#'car #'cadr)
|
|
|
|
(with-temp-buffer
|
|
|
|
(insert (org-babel-expand-body:lisp body params))
|
|
|
|
(funcall org-babel-lisp-eval-fn
|
|
|
|
`(swank:eval-and-grab-output
|
2016-09-22 17:45:15 +00:00
|
|
|
,(let ((dir (if (assq :dir params)
|
|
|
|
(cdr (assq :dir params))
|
2016-04-01 16:46:36 +00:00
|
|
|
default-directory)))
|
|
|
|
(format
|
|
|
|
(if dir (format org-babel-lisp-dir-fmt dir)
|
|
|
|
"(progn %s\n)")
|
|
|
|
(buffer-substring-no-properties
|
|
|
|
(point-min) (point-max)))))
|
2016-09-22 17:45:15 +00:00
|
|
|
(cdr (assq :package params)))))))
|
|
|
|
(org-babel-result-cond (cdr (assq :result-params params))
|
2019-09-07 18:03:46 +00:00
|
|
|
(org-strip-quotes result)
|
Backport changes from Emacs revs 115081 and 115082
2013-11-12 Stefan Monnier <monnier@iro.umontreal.ca>
Address some byte-compiler warnings.
* ob-abc.el (org-babel-expand-body:abc): Use dolist.
(org-babel-execute:abc): Fix regexp quoting.
* ob-calc.el (org--var-syms): Rename from `var-syms'.
* ob-lilypond.el (ly-compile-lilyfile): Remove redundant let-binding.
* ob-table.el (sbe): Move debug declaration.
* org-clock.el (org--msg-extra): Rename from `msg-extra'.
* org.el (org-version): Avoid var name starting with _.
(org-inhibit-startup, org-called-with-limited-levels)
(org-link-search-inhibit-query, org-time-was-given)
(org-end-time-was-given, org-def, org-defdecode, org-with-time):
* org-colview.el (org-agenda-overriding-columns-format):
* org-agenda.el (org-agenda-multi, org-depend-tag-blocked)
(org-agenda-show-log-scoped):
* ob-python.el (py-which-bufname, python-shell-buffer-name):
* ob-haskell.el (org-export-copy-to-kill-ring):
* ob-exp.el (org-link-search-inhibit-query):
* ob-R.el (ess-eval-visibly-p):
* ob-core.el (org-src-window-setup): Declare before use.
(org-babel-expand-noweb-references): Remove unused `blocks-in-buffer'.
* ox-odt.el (org-odt-hfy-face-to-css):
* org-src.el (org-src-associate-babel-session, org-src-get-lang-mode):
* org-bibtex.el (org-bibtex-get, org-bibtex-ask, org-bibtex)
(org-bibtex-check):
* ob-tangle.el (org-babel-tangle, org-babel-spec-to-string)
(org-babel-tangle-single-block, org-babel-tangle-comment-links):
* ob-table.el (sbe):
* ob-sqlite.el (org-babel-sqlite-expand-vars):
* ob-sql.el (org-babel-sql-expand-vars):
* ob-shen.el (org-babel-execute:shen):
* ob-sh.el (org-babel-execute:sh, org-babel-sh-evaluate):
* ob-scala.el (org-babel-scala-evaluate):
* ob-ruby.el (org-babel-ruby-table-or-string)
(org-babel-ruby-evaluate):
* ob-python.el (org-babel-python-table-or-string)
(org-babel-python-evaluate-external-process)
(org-babel-python-evaluate-session):
* ob-picolisp.el (org-babel-execute:picolisp):
* ob-perl.el (org-babel-perl-evaluate):
* ob-maxima.el (org-babel-execute:maxima):
* ob-lisp.el (org-babel-execute:lisp):
* ob-java.el (org-babel-execute:java):
* ob-io.el (org-babel-io-evaluate):
* ob-haskell.el (org-babel-execute:haskell):
* ob-fortran.el (org-babel-execute:fortran):
* ob-exp.el (org-babel-exp-code):
* ob-emacs-lisp.el (org-babel-execute:emacs-lisp):
* ob-ditaa.el (org-babel-execute:ditaa):
* ob-core.el (org-babel-execute-src-block, org-babel-sha1-hash)
(org-babel-parse-header-arguments, org-babel-reassemble-table)
(org-babel-goto-src-block-head, org-babel-mark-block)
(org-babel-expand-noweb-references, org-babel-script-escape)
(org-babel-process-file-name):
* ob-clojure.el (org-babel-execute:clojure):
* ob-calc.el (org-babel-execute:calc):
* ob-awk.el (org-babel-execute:awk):
* ob-abc.el (org-babel-execute:abc):
* ob-R.el (org-babel-expand-body:R):
* ob-C.el (org-babel-C-execute): Avoid deprecated ((lambda) ...).
2013-11-12 Glenn Morris <rgm@gnu.org>
* ox-html.el (org-html-scripts): Add 2013 to copyright years.
(org-html-infojs-template): Copyright holder to FSF.
2013-11-12 19:57:31 +00:00
|
|
|
(condition-case nil
|
2013-11-19 16:05:17 +00:00
|
|
|
(read (org-babel-lisp-vector-to-list result))
|
|
|
|
(error result))))
|
2016-09-22 17:45:15 +00:00
|
|
|
(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)
|
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
|