Mercurial > emacs
annotate lisp/org/ob-lisp.el @ 112402:a102f5772eaa
Merge from mainline.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Fri, 21 Jan 2011 12:27:07 -0800 |
parents | ef719132ddfa |
children |
rev | line source |
---|---|
111506
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
1 ;;; ob-lisp.el --- org-babel functions for Common Lisp |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
2 |
112275
6378d1b57038
Add 2011 to remaining FSF/AIST copyright years.
Glenn Morris <rgm@gnu.org>
parents:
111880
diff
changeset
|
3 ;; Copyright (C) 2010, 2011 Free Software Foundation |
111506
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
4 |
111880
a7740098b594
Update to Org mode 7.4
Carsten Dominik <carsten.dominik@gmail.com>
parents:
111521
diff
changeset
|
5 ;; Author: David T. O'Toole <dto@gnu.org>, Eric Schulte |
111506
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
6 ;; Keywords: literate programming, reproducible research, lisp |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
7 ;; Homepage: http://orgmode.org |
111880
a7740098b594
Update to Org mode 7.4
Carsten Dominik <carsten.dominik@gmail.com>
parents:
111521
diff
changeset
|
8 ;; Version: 7.4 |
111506
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
9 |
111880
a7740098b594
Update to Org mode 7.4
Carsten Dominik <carsten.dominik@gmail.com>
parents:
111521
diff
changeset
|
10 ;;; License: |
111506
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
11 |
111880
a7740098b594
Update to Org mode 7.4
Carsten Dominik <carsten.dominik@gmail.com>
parents:
111521
diff
changeset
|
12 ;; This program is free software; you can redistribute it and/or modify |
111506
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
13 ;; it under the terms of the GNU General Public License as published by |
111880
a7740098b594
Update to Org mode 7.4
Carsten Dominik <carsten.dominik@gmail.com>
parents:
111521
diff
changeset
|
14 ;; the Free Software Foundation; either version 3, or (at your option) |
a7740098b594
Update to Org mode 7.4
Carsten Dominik <carsten.dominik@gmail.com>
parents:
111521
diff
changeset
|
15 ;; any later version. |
a7740098b594
Update to Org mode 7.4
Carsten Dominik <carsten.dominik@gmail.com>
parents:
111521
diff
changeset
|
16 ;; |
a7740098b594
Update to Org mode 7.4
Carsten Dominik <carsten.dominik@gmail.com>
parents:
111521
diff
changeset
|
17 ;; This program is distributed in the hope that it will be useful, |
111506
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
20 ;; GNU General Public License for more details. |
111880
a7740098b594
Update to Org mode 7.4
Carsten Dominik <carsten.dominik@gmail.com>
parents:
111521
diff
changeset
|
21 ;; |
111506
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
22 ;; You should have received a copy of the GNU General Public License |
111880
a7740098b594
Update to Org mode 7.4
Carsten Dominik <carsten.dominik@gmail.com>
parents:
111521
diff
changeset
|
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
a7740098b594
Update to Org mode 7.4
Carsten Dominik <carsten.dominik@gmail.com>
parents:
111521
diff
changeset
|
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
a7740098b594
Update to Org mode 7.4
Carsten Dominik <carsten.dominik@gmail.com>
parents:
111521
diff
changeset
|
25 ;; Boston, MA 02110-1301, USA. |
111506
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
26 |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
27 ;;; Commentary: |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
28 |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
29 ;; Now working with SBCL for both session and external evaluation. |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
30 ;; |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
31 ;; This certainly isn't optimally robust, but it seems to be working |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
32 ;; for the basic use cases. |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
33 |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
34 ;;; Requirements: |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
35 |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
36 ;; Requires SLIME (Superior Lisp Interaction Mode for Emacs.) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
37 ;; See http://common-lisp.net/project/slime/ |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
38 |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
39 ;;; Code: |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
40 (require 'ob) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
41 (require 'ob-ref) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
42 (require 'ob-comint) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
43 (require 'ob-eval) |
111880
a7740098b594
Update to Org mode 7.4
Carsten Dominik <carsten.dominik@gmail.com>
parents:
111521
diff
changeset
|
44 |
a7740098b594
Update to Org mode 7.4
Carsten Dominik <carsten.dominik@gmail.com>
parents:
111521
diff
changeset
|
45 (declare-function slime-eval "ext:slime" (sexp &optional package)) |
a7740098b594
Update to Org mode 7.4
Carsten Dominik <carsten.dominik@gmail.com>
parents:
111521
diff
changeset
|
46 (declare-function slime-process "ext:slime" (&optional connection)) |
111508
45f3703e90f2
Fix ob-lisp.el expectation that slime would be available
Carsten Dominik <carsten.dominik@gmail.com>
parents:
111506
diff
changeset
|
47 (declare-function slime-connected-p "ext:slime" ()) |
111506
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
48 |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
49 (defvar org-babel-default-header-args:lisp '() |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
50 "Default header arguments for lisp code blocks.") |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
51 |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
52 (defcustom org-babel-lisp-cmd "sbcl --script" |
111880
a7740098b594
Update to Org mode 7.4
Carsten Dominik <carsten.dominik@gmail.com>
parents:
111521
diff
changeset
|
53 "Name of command used to evaluate lisp blocks." |
a7740098b594
Update to Org mode 7.4
Carsten Dominik <carsten.dominik@gmail.com>
parents:
111521
diff
changeset
|
54 :group 'org-babel |
a7740098b594
Update to Org mode 7.4
Carsten Dominik <carsten.dominik@gmail.com>
parents:
111521
diff
changeset
|
55 :type 'string) |
111506
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
56 |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
57 (defun org-babel-expand-body:lisp (body params) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
58 "Expand BODY according to PARAMS, return the expanded body." |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
59 (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
60 (if (> (length vars) 0) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
61 (concat "(let (" |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
62 (mapconcat |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
63 (lambda (var) (format "%S" (print `(,(car var) ',(cdr var))))) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
64 vars "\n ") |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
65 ")\n" body ")") |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
66 body))) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
67 |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
68 (defun org-babel-execute:lisp (body params) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
69 "Execute a block of Lisp code with org-babel. |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
70 This function is called by `org-babel-execute-src-block'" |
111880
a7740098b594
Update to Org mode 7.4
Carsten Dominik <carsten.dominik@gmail.com>
parents:
111521
diff
changeset
|
71 (require 'slime) |
111506
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
72 (message "executing Lisp source code block") |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
73 (let* ((session (org-babel-lisp-initiate-session |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
74 (cdr (assoc :session params)))) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
75 (result-type (cdr (assoc :result-type params))) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
76 (full-body (org-babel-expand-body:lisp body params))) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
77 (read |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
78 (if session |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
79 ;; session evaluation |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
80 (save-window-excursion |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
81 (cadr (slime-eval `(swank:eval-and-grab-output ,full-body)))) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
82 ;; external evaluation |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
83 (let ((script-file (org-babel-temp-file "lisp-script-"))) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
84 (with-temp-file script-file |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
85 (insert |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
86 ;; return the value or the output |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
87 (if (string= result-type "value") |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
88 (format "(print %s)" full-body) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
89 full-body))) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
90 (org-babel-eval |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
91 (format "%s %s" org-babel-lisp-cmd |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
92 (org-babel-process-file-name script-file)) "")))))) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
93 |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
94 ;; This function should be used to assign any variables in params in |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
95 ;; the context of the session environment. |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
96 (defun org-babel-prep-session:lisp (session params) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
97 "Prepare SESSION according to the header arguments specified in PARAMS." |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
98 (error "not yet implemented")) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
99 |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
100 (defun org-babel-lisp-initiate-session (&optional session) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
101 "If there is not a current inferior-process-buffer in SESSION |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
102 then create. Return the initialized session." |
111880
a7740098b594
Update to Org mode 7.4
Carsten Dominik <carsten.dominik@gmail.com>
parents:
111521
diff
changeset
|
103 (require 'slime) |
111506
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
104 (unless (string= session "none") |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
105 (save-window-excursion |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
106 (or (slime-connected-p) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
107 (slime-process))))) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
108 |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
109 (provide 'ob-lisp) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
110 |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
111 |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
112 ;;; ob-lisp.el ends here |