ob-lisp: Add SLY support
[org-mode/org-tableheadings.git] / lisp / ob-lisp.el
blob0ed4d843413d03fa9d60bc6b68cb161c452bd12c
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
6 ;; Eric Schulte
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/>.
26 ;;; Commentary:
28 ;;; Support for evaluating Common Lisp code, relies on SLY or SLIME
29 ;;; for all eval.
31 ;;; Requirements:
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/
38 ;;; Code:
39 (require 'ob)
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\"."
44 :group 'org-babel
45 :version "25.1"
46 :package-version '(Org . "8.3")
47 :options '("sly-eval" "slime-eval")
48 :type 'stringp)
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."
64 :group 'org-babel
65 :version "24.1"
66 :type '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)
73 (body (org-babel-trim
74 (if (> (length vars) 0)
75 (concat "(let ("
76 (mapconcat
77 (lambda (var)
78 (format "(%S (quote %S))" (car var) (cdr var)))
79 vars "\n ")
80 ")\n" body ")")
81 body))))
82 (if (or (member "code" result-params)
83 (member "pp" result-params))
84 (format "(pprint %s)" body)
85 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
92 ("slime-eval" 'slime)
93 ("sly-eval" 'sly)))
94 (org-babel-reassemble-table
95 (let ((result
96 (funcall (if (member "output" (cdr (assoc :result-params params)))
97 #'car #'cadr)
98 (with-temp-buffer
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))
104 default-directory)))
105 (format
106 (if dir (format org-babel-lisp-dir-fmt dir)
107 "(progn %s\n)")
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))
112 result
113 (condition-case nil
114 (read (org-babel-lisp-vector-to-list result))
115 (error 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))
125 (provide 'ob-lisp)
129 ;;; ob-lisp.el ends here