ob-lisp.el 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. (org-babel-reassemble-table
  51. (with-temp-buffer
  52. (insert (org-babel-expand-body:lisp body params))
  53. ((lambda (result)
  54. (if (member "output" (cdr (assoc :result-params params)))
  55. (car result)
  56. (condition-case nil
  57. (read (org-bable-lisp-vector-to-list (cadr result)))
  58. (error (cadr results)))))
  59. (slime-eval `(swank:eval-and-grab-output
  60. ,(buffer-substring-no-properties (point-min) (point-max)))
  61. (cdr (assoc :package params)))))
  62. (org-babel-pick-name (cdr (assoc :colname-names params))
  63. (cdr (assoc :colnames params)))
  64. (org-babel-pick-name (cdr (assoc :rowname-names params))
  65. (cdr (assoc :rownames params)))))
  66. (defun org-bable-lisp-vector-to-list (results)
  67. ;; TODO: better would be to replace #(...) with [...]
  68. (replace-regexp-in-string "#(" "(" results))
  69. (provide 'ob-lisp)
  70. ;; arch-tag: 18086168-009f-4947-bbb5-3532375d851d
  71. ;;; ob-lisp.el ends here