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 (defcustom org-babel-lisp-eval-fn
"sly-eval"
42 "The function to be called to evaluate code on the Lisp side.
43 It can be set to either \"sly-eval\" or \"slime-val\"."
46 :package-version
'(Org .
"8.3")
47 :options
'("sly-eval" "slime-eval")
50 (declare-function sly-eval
"ext:sly" (sexp &optional package
))
51 (declare-function slime-eval
"ext:slime" (sexp &optional package
))
53 (defvar org-babel-tangle-lang-exts
)
54 (add-to-list 'org-babel-tangle-lang-exts
'("lisp" .
"lisp"))
56 (defvar org-babel-default-header-args
:lisp
'())
57 (defvar org-babel-header-args
:lisp
'((package .
:any
)))
59 (defcustom org-babel-lisp-dir-fmt
60 "(let ((*default-pathname-defaults* #P%S\n)) %%s\n)"
61 "Format string used to wrap code bodies to set the current directory.
62 For example a value of \"(progn ;; %s\\n %%s)\" would ignore the
63 current directory string."
68 (defun org-babel-expand-body:lisp
(body params
)
69 "Expand BODY according to PARAMS, return the expanded body."
70 (let* ((vars (org-babel--get-vars params
))
71 (result-params (cdr (assoc :result-params params
)))
72 (print-level nil
) (print-length nil
)
74 (if (> (length vars
) 0)
78 (format "(%S (quote %S))" (car var
) (cdr var
)))
82 (if (or (member "code" result-params
)
83 (member "pp" result-params
))
84 (format "(pprint %s)" body
)
87 (defun org-babel-execute:lisp
(body params
)
88 "Execute a block of Common Lisp code with Babel.
89 BODY is the contents of the block, as a string. PARAMS is
90 a property list containing the parameters of the block."
91 (require (pcase org-babel-lisp-eval-fn
94 (org-babel-reassemble-table
96 (funcall (if (member "output" (cdr (assoc :result-params params
)))
99 (insert (org-babel-expand-body:lisp body params
))
100 (funcall org-babel-lisp-eval-fn
101 `(swank:eval-and-grab-output
102 ,(let ((dir (if (assoc :dir params
)
103 (cdr (assoc :dir params
))
106 (if dir
(format org-babel-lisp-dir-fmt dir
)
108 (buffer-substring-no-properties
109 (point-min) (point-max)))))
110 (cdr (assoc :package params
)))))))
111 (org-babel-result-cond (cdr (assoc :result-params params
))
114 (read (org-babel-lisp-vector-to-list result
))
116 (org-babel-pick-name (cdr (assoc :colname-names params
))
117 (cdr (assoc :colnames params
)))
118 (org-babel-pick-name (cdr (assoc :rowname-names params
))
119 (cdr (assoc :rownames params
)))))
121 (defun org-babel-lisp-vector-to-list (results)
122 ;; TODO: better would be to replace #(...) with [...]
123 (replace-regexp-in-string "#(" "(" results
))
129 ;;; ob-lisp.el ends here