Merge branch 'maint'
[org-mode.git] / lisp / ob-lisp.el
blobd98098e1361522fa611c2a12518c471d8434c724
1 ;;; ob-lisp.el --- Babel Functions for Common Lisp -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2017 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 <https://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 (declare-function sly-eval "ext:sly" (sexp &optional package))
42 (declare-function slime-eval "ext:slime" (sexp &optional package))
43 (declare-function org-trim "org" (s &optional keep-lead))
45 (defvar org-babel-tangle-lang-exts)
46 (add-to-list 'org-babel-tangle-lang-exts '("lisp" . "lisp"))
48 (defvar org-babel-default-header-args:lisp '())
49 (defvar org-babel-header-args:lisp '((package . :any)))
51 (defcustom org-babel-lisp-eval-fn #'slime-eval
52 "The function to be called to evaluate code on the Lisp side.
53 Valid values include `slime-eval' and `sly-eval'."
54 :group 'org-babel
55 :version "26.1"
56 :package-version '(Org . "9.0")
57 :type 'function)
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 (assq :result-params params)))
72 (print-level nil) (print-length nil)
73 (body (if (null vars) (org-trim body)
74 (concat "(let ("
75 (mapconcat
76 (lambda (var)
77 (format "(%S (quote %S))" (car var) (cdr var)))
78 vars "\n ")
79 ")\n" body ")"))))
80 (if (or (member "code" result-params)
81 (member "pp" result-params))
82 (format "(pprint %s)" body)
83 body)))
85 (defun org-babel-execute:lisp (body params)
86 "Execute a block of Common Lisp code with Babel.
87 BODY is the contents of the block, as a string. PARAMS is
88 a property list containing the parameters of the block."
89 (require (pcase org-babel-lisp-eval-fn
90 (`slime-eval 'slime)
91 (`sly-eval 'sly)))
92 (org-babel-reassemble-table
93 (let ((result
94 (funcall (if (member "output" (cdr (assq :result-params params)))
95 #'car #'cadr)
96 (with-temp-buffer
97 (insert (org-babel-expand-body:lisp body params))
98 (funcall org-babel-lisp-eval-fn
99 `(swank:eval-and-grab-output
100 ,(let ((dir (if (assq :dir params)
101 (cdr (assq :dir params))
102 default-directory)))
103 (format
104 (if dir (format org-babel-lisp-dir-fmt dir)
105 "(progn %s\n)")
106 (buffer-substring-no-properties
107 (point-min) (point-max)))))
108 (cdr (assq :package params)))))))
109 (org-babel-result-cond (cdr (assq :result-params params))
110 result
111 (condition-case nil
112 (read (org-babel-lisp-vector-to-list result))
113 (error result))))
114 (org-babel-pick-name (cdr (assq :colname-names params))
115 (cdr (assq :colnames params)))
116 (org-babel-pick-name (cdr (assq :rowname-names params))
117 (cdr (assq :rownames params)))))
119 (defun org-babel-lisp-vector-to-list (results)
120 ;; TODO: better would be to replace #(...) with [...]
121 (replace-regexp-in-string "#(" "(" results))
123 (provide 'ob-lisp)
127 ;;; ob-lisp.el ends here