org.el: Display images in link descriptions.
[org-mode.git] / lisp / ob-lisp.el
blob0f37653d9f1a40fc9f761a57ff342126793bb2d9
1 ;;; ob-lisp.el --- org-babel functions for common lisp evaluation
3 ;; Copyright (C) 2009-2013 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 slime for all eval
30 ;;; Requirements:
32 ;; Requires SLIME (Superior Lisp Interaction Mode for Emacs.)
33 ;; See http://common-lisp.net/project/slime/
35 ;;; Code:
36 (require 'ob)
38 (declare-function slime-eval "ext:slime" (sexp &optional package))
40 (defvar org-babel-tangle-lang-exts)
41 (add-to-list 'org-babel-tangle-lang-exts '("lisp" . "lisp"))
43 (defvar org-babel-default-header-args:lisp '())
44 (defvar org-babel-header-args:lisp '((package . :any)))
46 (defcustom org-babel-lisp-dir-fmt
47 "(let ((*default-pathname-defaults* #P%S)) %%s)"
48 "Format string used to wrap code bodies to set the current directory.
49 For example a value of \"(progn ;; %s\\n %%s)\" would ignore the
50 current directory string."
51 :group 'org-babel
52 :version "24.1"
53 :type 'string)
55 (defun org-babel-expand-body:lisp (body params)
56 "Expand BODY according to PARAMS, return the expanded body."
57 (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
58 (result-params (cdr (assoc :result-params params)))
59 (print-level nil) (print-length nil)
60 (body (org-babel-trim
61 (if (> (length vars) 0)
62 (concat "(let ("
63 (mapconcat
64 (lambda (var)
65 (format "(%S (quote %S))" (car var) (cdr var)))
66 vars "\n ")
67 ")\n" body ")")
68 body))))
69 (if (or (member "code" result-params)
70 (member "pp" result-params))
71 (format "(pprint %s)" body)
72 body)))
74 (defun org-babel-execute:lisp (body params)
75 "Execute a block of Common Lisp code with Babel."
76 (require 'slime)
77 (org-babel-reassemble-table
78 ((lambda (result)
79 (org-babel-result-cond (cdr (assoc :result-params params))
80 result
81 (condition-case nil
82 (if (member "output" (cdr (assoc :result-params params)))
83 ;; read printed output using normal org table parsing
84 (let ((tmp-file (org-babel-temp-file "lisp-output-")))
85 (with-temp-file tmp-file (insert result))
86 (org-babel-import-elisp-from-file tmp-file))
87 ;; read valued output as lisp
88 (read (org-babel-lisp-vector-to-list result)))
89 (error result))))
90 (funcall (if (member "output" (cdr (assoc :result-params params)))
91 #'car #'cadr)
92 (with-temp-buffer
93 (insert (org-babel-expand-body:lisp body params))
94 (slime-eval `(swank:eval-and-grab-output
95 ,(let ((dir (if (assoc :dir params)
96 (cdr (assoc :dir params))
97 default-directory)))
98 (format (format org-babel-lisp-dir-fmt dir)
99 (buffer-substring-no-properties
100 (point-min) (point-max)))))
101 (cdr (assoc :package params))))))
102 (org-babel-pick-name (cdr (assoc :colname-names params))
103 (cdr (assoc :colnames params)))
104 (org-babel-pick-name (cdr (assoc :rowname-names params))
105 (cdr (assoc :rownames params)))))
107 (defun org-babel-lisp-vector-to-list (results)
108 ;; TODO: better would be to replace #(...) with [...]
109 (replace-regexp-in-string "#(" "(" results))
111 (provide 'ob-lisp)
115 ;;; ob-lisp.el ends here