ob-lisp.el 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. ;;; ob-lisp.el --- org-babel functions for Common Lisp
  2. ;; Copyright (C) 2010 Free Software Foundation
  3. ;; Author: David T. O'Toole <dto@gnu.org>, Eric Schulte
  4. ;; Keywords: literate programming, reproducible research, lisp
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 7.5
  7. ;;; License:
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; Now working with SBCL for both session and external evaluation.
  24. ;;
  25. ;; This certainly isn't optimally robust, but it seems to be working
  26. ;; for the basic use cases.
  27. ;;; Requirements:
  28. ;; Requires SLIME (Superior Lisp Interaction Mode for Emacs.)
  29. ;; See http://common-lisp.net/project/slime/
  30. ;;; Code:
  31. (require 'ob)
  32. (require 'ob-ref)
  33. (require 'ob-comint)
  34. (require 'ob-eval)
  35. (declare-function slime-eval "ext:slime" (sexp &optional package))
  36. (declare-function slime-process "ext:slime" (&optional connection))
  37. (declare-function slime-connected-p "ext:slime" ())
  38. (defvar org-babel-default-header-args:lisp '()
  39. "Default header arguments for lisp code blocks.")
  40. (defcustom org-babel-lisp-cmd "sbcl --script"
  41. "Name of command used to evaluate lisp blocks."
  42. :group 'org-babel
  43. :type 'string)
  44. (defun org-babel-expand-body:lisp (body params)
  45. "Expand BODY according to PARAMS, return the expanded body."
  46. (let ((vars (mapcar #'cdr (org-babel-get-header params :var))))
  47. (if (> (length vars) 0)
  48. (concat "(let ("
  49. (mapconcat
  50. (lambda (var) (format "%S" (print `(,(car var) ',(cdr var)))))
  51. vars "\n ")
  52. ")\n" body ")")
  53. body)))
  54. (defun org-babel-execute:lisp (body params)
  55. "Execute a block of Lisp code with org-babel.
  56. This function is called by `org-babel-execute-src-block'"
  57. (require 'slime)
  58. (message "executing Lisp source code block")
  59. (let* ((session (org-babel-lisp-initiate-session
  60. (cdr (assoc :session params))))
  61. (result-type (cdr (assoc :result-type params)))
  62. (full-body (org-babel-expand-body:lisp body params)))
  63. (read
  64. (if session
  65. ;; session evaluation
  66. (save-window-excursion
  67. (cadr (slime-eval `(swank:eval-and-grab-output ,full-body))))
  68. ;; external evaluation
  69. (let ((script-file (org-babel-temp-file "lisp-script-")))
  70. (with-temp-file script-file
  71. (insert
  72. ;; return the value or the output
  73. (if (string= result-type "value")
  74. (format "(print %s)" full-body)
  75. full-body)))
  76. (org-babel-eval
  77. (format "%s %s" org-babel-lisp-cmd
  78. (org-babel-process-file-name script-file)) ""))))))
  79. ;; This function should be used to assign any variables in params in
  80. ;; the context of the session environment.
  81. (defun org-babel-prep-session:lisp (session params)
  82. "Prepare SESSION according to the header arguments specified in PARAMS."
  83. (error "not yet implemented"))
  84. (defun org-babel-lisp-initiate-session (&optional session)
  85. "If there is not a current inferior-process-buffer in SESSION
  86. then create. Return the initialized session."
  87. (require 'slime)
  88. (unless (string= session "none")
  89. (save-window-excursion
  90. (or (slime-connected-p)
  91. (slime-process)))))
  92. (provide 'ob-lisp)
  93. ;; arch-tag: 18086168-009f-4947-bbb5-3532375d851d
  94. ;;; ob-lisp.el ends here