1 ;;; ob-lisp.el --- Babel Functions for Common Lisp -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2016 Free Software Foundation, Inc.
5 ;; Authors: Joel Boehland
7 ;; David T. O'Toole <dto@gnu.org>
8 ;; Keywords: literate programming, reproducible research
9 ;; Homepage: http://orgmode.org
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;;; Support for evaluating Common Lisp code, relies on SLY or SLIME
33 ;; Requires SLY (Sylvester the Cat's Common Lisp IDE) or SLIME
34 ;; (Superior Lisp Interaction Mode for Emacs). See:
35 ;; - https://github.com/capitaomorte/sly
36 ;; - http://common-lisp.net/project/slime/
41 (declare-function sly-eval
"ext:sly" (sexp &optional package
))
42 (declare-function slime-eval
"ext:slime" (sexp &optional package
))
44 (defvar org-babel-tangle-lang-exts
)
45 (add-to-list 'org-babel-tangle-lang-exts
'("lisp" .
"lisp"))
47 (defvar org-babel-default-header-args
:lisp
'())
48 (defvar org-babel-header-args
:lisp
'((package .
:any
)))
50 (defcustom org-babel-lisp-eval-fn
#'slime-eval
51 "The function to be called to evaluate code on the Lisp side.
52 Valid values include `slime-eval' and `sly-eval'."
55 :package-version
'(Org .
"9.0")
58 (defcustom org-babel-lisp-dir-fmt
59 "(let ((*default-pathname-defaults* #P%S\n)) %%s\n)"
60 "Format string used to wrap code bodies to set the current directory.
61 For example a value of \"(progn ;; %s\\n %%s)\" would ignore the
62 current directory string."
67 (defun org-babel-expand-body:lisp
(body params
)
68 "Expand BODY according to PARAMS, return the expanded body."
69 (let* ((vars (org-babel--get-vars params
))
70 (result-params (cdr (assoc :result-params params
)))
71 (print-level nil
) (print-length nil
)
73 (if (> (length vars
) 0)
77 (format "(%S (quote %S))" (car var
) (cdr var
)))
81 (if (or (member "code" result-params
)
82 (member "pp" result-params
))
83 (format "(pprint %s)" body
)
86 (defun org-babel-execute:lisp
(body params
)
87 "Execute a block of Common Lisp code with Babel.
88 BODY is the contents of the block, as a string. PARAMS is
89 a property list containing the parameters of the block."
90 (require (pcase org-babel-lisp-eval-fn
93 (org-babel-reassemble-table
95 (funcall (if (member "output" (cdr (assoc :result-params params
)))
98 (insert (org-babel-expand-body:lisp body params
))
99 (funcall org-babel-lisp-eval-fn
100 `(swank:eval-and-grab-output
101 ,(let ((dir (if (assoc :dir params
)
102 (cdr (assoc :dir params
))
105 (if dir
(format org-babel-lisp-dir-fmt dir
)
107 (buffer-substring-no-properties
108 (point-min) (point-max)))))
109 (cdr (assoc :package params
)))))))
110 (org-babel-result-cond (cdr (assoc :result-params params
))
113 (read (org-babel-lisp-vector-to-list result
))
115 (org-babel-pick-name (cdr (assoc :colname-names params
))
116 (cdr (assoc :colnames params
)))
117 (org-babel-pick-name (cdr (assoc :rowname-names params
))
118 (cdr (assoc :rownames params
)))))
120 (defun org-babel-lisp-vector-to-list (results)
121 ;; TODO: better would be to replace #(...) with [...]
122 (replace-regexp-in-string "#(" "(" results
))
128 ;;; ob-lisp.el ends here