Merge branch 'maint'
[org-mode/org-tableheadings.git] / lisp / ob-lisp.el
blob1e781ff363c7f343e0d0ebaa73ab0f924af1db88
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 (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'."
53 :group 'org-babel
54 :version "25.1"
55 :package-version '(Org . "9.0")
56 :type 'function)
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."
63 :group 'org-babel
64 :version "24.1"
65 :type '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)
72 (body (org-babel-trim
73 (if (> (length vars) 0)
74 (concat "(let ("
75 (mapconcat
76 (lambda (var)
77 (format "(%S (quote %S))" (car var) (cdr var)))
78 vars "\n ")
79 ")\n" body ")")
80 body))))
81 (if (or (member "code" result-params)
82 (member "pp" result-params))
83 (format "(pprint %s)" body)
84 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
91 (`slime-eval 'slime)
92 (`sly-eval 'sly)))
93 (org-babel-reassemble-table
94 (let ((result
95 (funcall (if (member "output" (cdr (assoc :result-params params)))
96 #'car #'cadr)
97 (with-temp-buffer
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))
103 default-directory)))
104 (format
105 (if dir (format org-babel-lisp-dir-fmt dir)
106 "(progn %s\n)")
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))
111 result
112 (condition-case nil
113 (read (org-babel-lisp-vector-to-list result))
114 (error 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))
124 (provide 'ob-lisp)
128 ;;; ob-lisp.el ends here