ob-lisp.el 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. ;;; ob-lisp.el --- org-babel functions for common lisp evaluation
  2. ;; Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
  3. ;; Author: Joel Boehland, Eric Schulte, David T. O'Toole <dto@gnu.org>
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 7.5
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs 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 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;;; support for evaluating common lisp code, relies on slime for all eval
  20. ;;; Requirements:
  21. ;; Requires SLIME (Superior Lisp Interaction Mode for Emacs.)
  22. ;; See http://common-lisp.net/project/slime/
  23. ;;; Code:
  24. (require 'ob)
  25. (declare-function slime-eval "ext:slime" (sexp &optional package))
  26. (add-to-list 'org-babel-tangle-lang-exts '("lisp" . "lisp"))
  27. (defvar org-babel-default-header-args:lisp '())
  28. (defvar org-babel-header-arg-names:lisp '(package))
  29. (defun org-babel-expand-body:lisp (body params)
  30. "Expand BODY according to PARAMS, return the expanded body."
  31. (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
  32. (result-params (cdr (assoc :result-params params)))
  33. (print-level nil) (print-length nil)
  34. (body (org-babel-trim
  35. (if (> (length vars) 0)
  36. (concat "(let ("
  37. (mapconcat
  38. (lambda (var)
  39. (format "(%S (quote %S))" (car var) (cdr var)))
  40. vars "\n ")
  41. ")\n" body ")")
  42. (format "(progn %s)" body)))))
  43. (if (or (member "code" result-params)
  44. (member "pp" result-params))
  45. (format "(pprint %s)" body)
  46. body)))
  47. (defun org-babel-execute:lisp (body params)
  48. "Execute a block of Common Lisp code with Babel."
  49. (require 'slime)
  50. (with-temp-buffer
  51. (insert (org-babel-expand-body:lisp body params))
  52. ((lambda (result)
  53. (if (member "output" (cdr (assoc :result-params params)))
  54. (car result)
  55. (condition-case nil (read (cadr result)) (error (cadr result)))))
  56. (slime-eval `(swank:eval-and-grab-output
  57. ,(buffer-substring-no-properties (point-min) (point-max)))
  58. (cdr (assoc :package params))))))
  59. (provide 'ob-lisp)
  60. ;; arch-tag: 18086168-009f-4947-bbb5-3532375d851d
  61. ;;; ob-lisp.el ends here